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?
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')
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.
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) {
//...
}
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)