Unable to call method from a different class

Godot Version

4.3

Question

I’m trying to call a public method from a different class, however it’s not working. Currently I’m using the code below:

Method (stored in Bucket.cs)

And this is where the method is called (in Enemy.cs)

At the moment, an error is created every frame (since whatever is creating the error is in the _Process area) that states:
“Object reference not set to an instance of an object.”

It’s also worth noting, that the method is called from an instance that does not exist in the scene tree when the game starts.

Can anyone help with what is probably a very easy solution?

Hello,

You create local variable, and the value won’t be assigned to class variable private Bucket bucket.
Instead of var bucket = (Bucket)root.GetNode('Bucket') use bucket = (Bucket)root.GetNode('Bucket')

1 Like

Hi,

Thanks for your help!

Unfortunately, this hasn’t worked. Removing the var has lead to the exact same problem.

Thank you, regardless :slight_smile:

Are you sure that the Bucket scene is in the correct place in scene tree?
Please under line
bucket = (Bucket)root.GetNode('Bucket')
write
GD.Print(bucket)

If it prints null, the problem is with the scene tree and (Bucket)root.GetNode('Bucket') cannot find scene.

GD.Print(bucket)
returns
<CharacterBody2D#29376906513>
to the console.
Also, in the remote menu of the scene tree (shown when the game is running), Bucket appears above Enemy. They also have the same parent of Main.

1 Like

On which line do you have this error?

Not stated, I assume it’s line 23, with some investigative work from a GD.Print() statement.


Please expand single error. Send error Stack Trace.

E 0:00:01:0031 void Enemy._PhysicsProcess(double): System.NullReferenceException: Object reference not set to an instance of an object.
<C# Error> System.NullReferenceException
<C# Source> Enemy.cs:23 @ void Enemy._PhysicsProcess(double)
Enemy.cs:23 @ void Enemy._PhysicsProcess(double)
Node.cs:2389 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CanvasItem.cs:1505 @ bool Godot.CanvasItem.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
Node2D.cs:546 @ bool Godot.Node2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CollisionObject2D.cs:678 @ bool Godot.CollisionObject2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
PhysicsBody2D.cs:113 @ bool Godot.PhysicsBody2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CharacterBody2D.cs:786 @ bool Godot.CharacterBody2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
Enemy_ScriptMethods.generated.cs:48 @ bool Enemy.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)

Please, print bucket in method _PhysicsProcess().

As the error suggests your problem is in line 23. You are trying to access a member on a null reference. So the problem is not that bucket is null but collisionData is, i.e., you are calling GetCollider() on null.

Btw. as far as I’m aware Godot does not provide a custom Equals implementation for their objects, so it is the same as comparing the reference with == (if you don’t provide your own implementation):

if (collisionData?.GetCollider() == bucket) {
 //...
}

Bingo, that’s it! Thank you! Printing collisionData to console returns null.

And, the Equals reference was a desperate attempt to fix it, thinking that I somehow was just using the wrong technique. It is the base C# Type.Equals method there.

Out of interest, would you know why collisionData is null? (I have also since added MoveAndSlide() to the end of _PhysicsProcess to the same result)

According to the documentation at CharacterBody2D — Godot Engine (stable) documentation in English

Returns a KinematicCollision2D, which contains information about the latest collision that occurred during the last call to move_and_slide.

So i guess if there was no collision, there is probably no collision data.

Ok, that’s a bit weird.

Anyway, thank you very much for your help! I think I know how to progress from here! :slight_smile:

You’ve both been very useful.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.