Godot Version
3.5.1
Question
So I’m making a tiny game (to learn Godot) and in it there are 4 areas; I’m trying to make it so that each area has its own 3 collectibles and the bar displaying the collectibles changes depending on the area you’re in. I’ve currently set this up in a way where i have 4 separate control nodes (bars) that each have the same script (shown below), each of these bars have a sprite 2d encased in a control node (do not ask me why I am very new to UI and Godot in general and i made this code quite a while ago, but i doubt its related to the issue). So I’ve made code that imports all of these sprite 2Ds and the bars into the script (attached to each bar) and then places them into arrays. And then I use functions that have parameters that tell which fruit to change/which bar to make visible/invisible and then calculate exactly which one by using indexes such as [area - 1] (I hope this makes sense), to then change the properties of that object. so for example id do “apples[area - 1].frame = frame” (frame is a parameter). But this always gives me an error such as, “Invalid assignment of property or key ‘frame’ with value of type ‘int’ on a base object of type ‘Nil’”.
I tried my best to explicitly set the type of each object withing the array but I’m not sure why the
type is null. Is there any way to fix this or a better way to implement a feature like this? Can you not change the properties of an object in an array of a non primitive type?
Heres the code:
extends Node
@onready var Bar1 : Control = %Area1Bar
@onready var Bar2 : Control = %Area2Bar
@onready var Bar3 : Control = %Area3Bar
@onready var Bar4 : Control = %Area4Bar#The bars above are accessed with unique names
@onready var green_circle_fruit: Sprite2D = $“../Area1Bar/Control/GreenCircleFruit”
@onready var green_tall_fruit: Sprite2D = $“../Area1Bar/Control2/GreenTallFruit”
@onready var green_grape_fruit: Sprite2D = $“../Area1Bar/Control3/GreenGrapeFruit”
@onready var yellow_circle_fruit: Sprite2D = $“../Area2Bar/Control/YellowCircleFruit”
@onready var yellow_tall_fruit: Sprite2D = $“../Area2Bar/Control2/YellowTallFruit”
@onready var yellow_grape_fruit: Sprite2D = $“../Area2Bar/Control3/YellowGrapeFruit”
@onready var pink_circle_fruit: Sprite2D = $“../Area3Bar/Control/PinkCircleFruit”
@onready var pink_tall_fruit: Sprite2D = $“../Area3Bar/Control2/PinkTallFruit”
@onready var pink_grape_fruit: Sprite2D = $“../Area3Bar/Control3/PinkGrapeFruit”
@onready var red_circle_fruit: Sprite2D = $“../Area4Bar/Control/RedCircleFruit”
@onready var red_tall_fruit: Sprite2D = $“../Area4Bar/Control2/RedTallFruit”
@onready var red_grape_fruit: Sprite2D = $“../Area4Bar/Control3/RedGrapeFruit”var bars : Array[Control] = [Bar1, Bar2, Bar3, Bar4]
var apples : Array[Sprite2D]= [green_circle_fruit, yellow_circle_fruit, pink_circle_fruit, red_grape_fruit]
var pears : Array[Sprite2D]= [green_tall_fruit, yellow_tall_fruit, pink_tall_fruit, red_tall_fruit]
var grapes : Array[Sprite2D]= [green_grape_fruit, yellow_grape_fruit, pink_grape_fruit, red_grape_fruit]
func BarChange(area):
bars[area - 1].visible = false
bars[area].visible = truethis line gives me the error: BarChange: Invalid assignment of property or key ‘visible’ with value of type ‘bool’ on a base object of type ‘Nil’.
func FruitChange(fruit, frame, area):
print(fruit)
if fruit == “apple”:
print(“apple collected”)
apples[area - 1].frame = frame
elif fruit == “pear”:
print(“paer collected”)
pears[area - 1].frame = frame
elif fruit == “grape”:
print(“grape collected”)
grapes[area - 1].frame = framethese lines give me the error: FruitChange: Invalid assignment of property or key ‘frame’ with value of type ‘int’ on a base object of type ‘Nil’.