Hello, in the DOC of the game Dodge the creeps indicate this line:

But that line causes this failure:

I tried this:

But this error appears

Link with the project: https://www.dropbox.com/s/p6fce9hca01khmd/dodge%20the%20creeps.rar?dl=0

How should I solve this? I think you have to modify that part of the official DOC of the game Dodge the creeps! in C #. https://docs.godotengine.org/en/3.1/getting_started/step_by_step/your_first_game.html

With the code from the documentation, did you assign the Mob variable to the Mob scene in the editor? That might be why you are getting the null reference exception error.

With the other error, I'm not totally sure to be honest. I have not really done very much C# programming in Godot. Looking at the error, I think the issue is happening because either ResourceLoader.load("res://Mob.tscn") cannot be cased to a PackedScene, or because enemigo.Instance() cannot be cast to a RigidBody2D. Unfortunately, I am not sure on what to do to fix it.

The only thing I can think of that might help with figuring out what exactly is causing the problem is to comment-out the code and slowly reintroduce it. That might help with figuring out which line of code is causing the problem, which in turn might help with finding a solution.

@TwistedTwigleg said: With the code from the documentation, did you assign the Mob variable to the Mob scene in the editor? That might be why you are getting the null reference exception error.

With the other error, I'm not totally sure to be honest. I have not really done very much C# programming in Godot. Looking at the error, I think the issue is happening because either ResourceLoader.load("res://Mob.tscn") cannot be cased to a PackedScene, or because enemigo.Instance() cannot be cast to a RigidBody2D. Unfortunately, I am not sure on what to do to fix it.

The only thing I can think of that might help with figuring out what exactly is causing the problem is to comment-out the code and slowly reintroduce it. That might help with figuring out which line of code is causing the problem, which in turn might help with finding a solution.

Hey, thanks for the help. The Mob variable did not have the scene loaded in the editor, I have already done it and saved the scene but the casting error still exists.

The error is occurring on the line: AddChild (mobInstance); If I comment that line the error does not occur.

Mob scene:

---

The if is true (The casting error keeps happening) and GD.print writes "Mob" type: It is right?.

---

Here I found a similar error but I have the names correctly. The Mob scene script is also called Mob. https://godotengine.org/qa/29937/getting-invalidcastingexceptions-when-instancing-scene

@TheMnk said: The Mob variable did not have the scene loaded in the editor, I have already done it and saved the scene but the casting error still exists.

Cool, now we know that the exported variable is not causing the issue. :+1:

The error is occurring on the line: AddChild (mobInstance);

Interesting. If I had to guess, then the error is probably caused by mobInstance being null, probably from the casting. That said, I would think that the casting would work, since the Mob script extends a RigidBody2D node.

The if is true (The casting error keeps happening) and GD.print writes "Mob" type: It is right?.

I think so. I'll download the project and the mono version of Godot and see if I can help figure it out.

Ah ha! I figured it out. While the debugger says the issue is within Main.cs, the code there is fine. The issue is actually in Mob.cs.

The reason the casting does not work is because Mob.cs fails when the project is running. This happens because the Visibility node is being casted to a VisibilityNotifier node, while it actually needs to be casted to a VisibilityNotifier2D node. All you need to do is change the _ready function in Mob.cs to the following:

public override void _Ready()
    {
        //GetNode<AnimatedSprite>("AnimatedSprite").Animation = _mobTypes[GD.Randi() % _mobTypes.Length];
        GetNode<AnimatedSprite>("AnimatedSprite").Animation = _mobTypes[numAleatorio.Next(0, _mobTypes.Length - 1)];
        GetNode<VisibilityNotifier2D>("Visibility").Connect("screen_exited", this, "OnVisibilityScreenExited");
    }

Then it should work :smile:

@TwistedTwigleg said: Ah ha! I figured it out. While the debugger says the issue is within Main.cs, the code there is fine. The issue is actually in Mob.cs.

The reason the casting does not work is because Mob.cs fails when the project is running. This happens because the Visibility node is being casted to a VisibilityNotifier node, while it actually needs to be casted to a VisibilityNotifier2D node. All you need to do is change the _ready function in Mob.cs to the following:

public override void _Ready()
    {
        //GetNode<AnimatedSprite>("AnimatedSprite").Animation = _mobTypes[GD.Randi() % _mobTypes.Length];
        GetNode<AnimatedSprite>("AnimatedSprite").Animation = _mobTypes[numAleatorio.Next(0, _mobTypes.Length - 1)];
        GetNode<VisibilityNotifier2D>("Visibility").Connect("screen_exited", this, "OnVisibilityScreenExited");
    }

Then it should work :smile:

Ok I have already seen it. I didn't realize that mistake. Thank you!. I think we have not discovered any bugs and there is nothing that should be reported.

3 years later