Sibling Node is Null

Godot Version

4.1.2.stable.mono

Goal

image
I’m trying to get the Raycasts node from the script of the Brain node by doing

var raycasts : Node = get_node("../Raycasts")

under my _init() function in my brain class.

Problem

When I do:

for i in RaycastsAmount:
	var raycast : RayCast2D = RayCast2D.new()
	
	raycast.set_target_position(Vector2(
		sin((i / RaycastsAmount) * PI * 2) * RaycastsDistance,
		cos((i / RaycastsAmount) * PI * 2) * RaycastsDistance))
	
	raycasts.add_child(raycast)

I get this error:
Cannot call method add_child on a null value.
and when I print raycasts, it says it’s <Object#null>

I also tried doing

var raycasts : Node = $"../Raycasts"

but that gave me the same error.

What am I doing wrong?

Usually Nodes aren’t ready when their _init() function fires. So the issues seems to be that because you are doing this in the _init() then the Sibling node isn’t ready.
You could move this whole process outside of the _init() and do it within the _ready(). There shouldnt be any difference in the whole process as doing it within the _init() really isn’t necessary.

1 Like

I agree with what Triberium wrote. You could also just create an Export and assign the RayCast in the Inspector for a quick fix.

1 Like

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