Reputation: 1
So, I am trying to make a platformer game using the Godot engine, and I am using C#. Every time I run the code, I am given this message:
"E 0:00:00:0740 can_instantiate: Cannot instance script because the associated class could not be found. Script: 'res://player.tscn::CSharpScript_qaa3r'. Make sure the script exists and contains a class definition with a name that matches the filename of the script exactly (it's case-sensitive). <C++ Error> Method/function failed. Returning: false <C++ Source> modules/mono/csharp_script.cpp:2417 @ can_instantiate()
This is my code so far:
using Godot;
using System;
public class Player : CharacterBody2D{
[Export]
public int Speed = 200;
[Export]
public int JumpForce = 300;
[Export]
public int Gravity = 10;
private Vector2 _velocity = new Vector2();
public override void _Ready(){
GD.Print("Player is ready!");
_velocity = Vector2.Zero;
}
public override void _PysicsProcess (float delta){
_velocity.y += Gravity * delta;
_velocity.x = 0;
if(Input.IsActionPressed("move_right")){
_velocity.x += Speed;
}
if(Input.IsActionPressed("move_left")){
_velocity.x -= Speed;
}
if(Input.IsActionPressed("move_jump") && IsOnFloor()){
_velocity.y = -JumpForce;
}
_velocity = MoveAndSlide(_velocity, Vector2.Up);
}
}
This is titled "Player.cs" and is part of the player.tscn scene
The initialization code:
using Godot;
using System;
public class GameManager : Node
{
// Declare variables for your player and other nodes
private Player _player;
private Node _otherNode;
public override void _Ready()
{
// Initialize player and other nodes
InitializePlayer();
InitializeOtherNodes();
}
private void InitializePlayer()
{
// Load the player scene (assuming you have a player scene saved as "res://Player.tscn")
PackedScene playerScene = (PackedScene)GD.Load("res://Player.tscn");
if (playerScene != null)
{
_player = (Player)playerScene.Instance();
AddChild(_player);
_player.Position = new Vector2(100, 100); // Set initial position
}
else
{
GD.PrintErr("Failed to load player scene.");
}
}
private void InitializeOtherNodes()
{
// Load other nodes or game objects
PackedScene otherNodeScene = (PackedScene)GD.Load("res://OtherNode.tscn");
if (otherNodeScene != null)
{
_otherNode = otherNodeScene.Instance();
AddChild(_otherNode);
_otherNode.Position = new Vector2(200, 200); // Set initial position
}
else
{
GD.PrintErr("Failed to load other node scene.");
}
}
}
If someone knows why this is happening that would be fantastic. Thank you!
I've tried changing the case in the initialization code from player.tscn to Player.tscn but that didn't change the error. I'm really stuck here so any help would be helpful. Thank you again!
Upvotes: 0
Views: 215
Reputation: 19
Besides the typo in Player.cs:
_PysicsProcess // should be _PhysicsProcess
It seems like this is more of a structure problem. At first glance the error indicates that it can't find the script, since you have the naming of it correct (in C# class definitions and filenames need to match), it seems to me like you may have forgotten to attach the script to the Player.tscn node.
Or if that is set correctly, try to delete both the script and the node, and remake them. When typing the script, please avoid copy pasting the entire file, as this can sometimes paste artifacts that are interpreted by the compiler but are not easily detectable.
Upvotes: 0