how do i access nodes variables in an array?

Godot Version

4.5.stable

Question

hi, i have this code here to make a plant grow whenever a certain amount of days have passed

extends Node2D

const growthTime: int = 1 #days needed between growth cycles

var growth: int = 1
var currentGrowth: int = growthTime

@onready var stage_1: Sprite2D = $Stage1
@onready var stage_2: Sprite2D = $Stage2
@onready var stage_3: Sprite2D = $Stage3
@onready var stage_4: Sprite2D = $Stage4

var GrowthStages: Array = [stage_1,stage_2,stage_3,stage_4]

func _ready() -> void:
	DayAndNight.day_ended.connect(onDayEnded)

func onDayEnded():
	currentGrowth -= 1
	if currentGrowth == 0:
		GrowthStages[growth].visible = false
		GrowthStages[growth + 1].visible = true
		growth += 1

however, when i run the program and i reach this line of code here

GrowthStages[growth].visible = false

it gives me this error

Invalid assignment of property or key ‘visible’ with value of type ‘bool’ on a base object of type ‘Nil’.

is there a way to access a node variable in an array or am i just doing this wrong?

your array is not @onready so it is assigned before the stage_x are, resulting in an Array of null values.

1 Like

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