Adding elements from one array to another array makes nothing (<Object#null>)

Godot Version

Question

I need multiple arrays:
bad_food is used to store elements (buttons) which are not on the scene
food is to store elements which are on the scene

extends Node2D
var bad_food = [$Button1, $Button1, $Button3]

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	randomize()
	var food = []
	food.push_back(bad_food.pick_random())
	bad_food.erase(bad_food.pick_random())
	food.push_back(bad_food.pick_random())
	print(food)

In the end console printing: [<Object#null>, <Object#null>] and giving an error:
E 0:00:00:0606 Button_random.gd:2 @ @implicit_new(): Node not found: “Button1” (relative to “Node2D”).

I think that error is saying the $ syntax failed to get a node. Where are your buttons relative to your Node2D in the scene tree structure?

1 Like

Assuming the NodePaths are correct you’ll need to use the @onready annotation because the node won’t have a reference to the SceneTree before that.

1 Like

They there in the Node2D like that:
image

Everything started to work by connecting to the _ready func there referenced all arrays.

Thanks! It’s working!

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