Node isnt being found

For some reason my code is broken and cannot find the all my @onready nodes. HELP!
Github link to the game: GitHub - kaihan-man/BurgerClickerGame: This is for godot

extends Button

@onready var burger: AnimatedSprite2D = $burger
@onready var grease_counter: Label = $greaseCounter
@onready var click_multipler_button: Button = $ClickMultiplerButton
@onready var click_sound_effect: AudioStreamPlayer2D = $ClickSoundEffect

var rng = RandomNumberGenerator.new()

func _ready() -> void:
	assert(burger, "Node 'burger' not found!")
	assert(grease_counter, "Node 'greaseCounter' not found!")
	assert(click_multipler_button, "Node 'ClickMultiplerButton' not found!")
	assert(click_sound_effect, "Node 'ClickSoundEffect' not found!")

func _on_pressed() -> void:
	Global.greasePoints += Global.clickMultiplier
	grease_counter.text = str(round(Global.greasePoints))
	click_sound_effect.play()
	burger.scale.x = 0.28
	burger.scale.y = 0.28
	await get_tree().create_timer(0.1).timeout
	burger.scale.x = 0.3
	burger.scale.y = 0.3

func _on_click_multipler_pressed() -> void:
	rng.randomize()
	if Global.greasePoints >= Global.clickPrice:
		Global.clickMultiplier += 1
		Global.greasePoints -= Global.clickPrice
		grease_counter.text = str(round(Global.greasePoints))
		Global.clickPrice *= rng.randf_range(1.4, 2.1)
		click_multipler_button.text = "+1 Grease Point Per Click\nCost: " + str(round(Global.clickPrice))

You’re trying to reference siblings of this Node by using the syntax for children, and you’re using incorrect names - the name has to be exact to work properly.
E.g. the “burger” node should be $"../Burger" instead of $burger

I would also say it’s better to use @export var sytax than @onready var, it gives you better control over which Nodes you are referencing. But this is just a preference thing in the end.

It didnt work when I used $“…/burger”. it said the exact same thing when I used $burger and I don’t know how @export works like when I used it there were lots of errors. Could you show me how the correct line of code would look like?

Seems like you should attach this script to the start screen’s root node instead of the play button. You can still connect the pressed signal to this script.

ok, done.

Like I said, you need an exact name.
$“…/burger” is not the same as $“…/Burger”, you need to be precise with capitalization.

@export var can be used like that, the values are then assigned in the Inspector (see the far right side of the screen).


I don’t know which Nodes you want to assign to grease_counter, click_multipler_button or click_sound_effect variables, because nodes of these types don’t exist in the current scene. If you want to reference Nodes in the MainGame scene, maybe this script (at least part of it) should be somewhere on the MainGame scene instead of the StartingScene?
It’s kinda confusing how you’re referencing your Nodes all over the place.

im not, the MainGame (click_button) script is only referencing from the main game scene. im not doing anything with the starting screen scene.

Well, this is the click_button.gd script and it’s referenced only within the starting_screen.tscn, not the main_game.tscn. Maybe you mixed up your button scripts.
obraz

How would I fix it so its refrencing only things in the main game because it doesnt even know the main game nodes. I updated my github.

You need to do a couple changes for this to work:

  1. In starting_screen.tscn scene: change the script on the PlayButton Node to play_button
    obraz
  2. In main_game.tscn scene: change the pressed signal connection to the _on_click_multipler_pressed callable on the MainGame Node
  3. In main_game.gd script: change the Node type from Button to Node2D and change the burger variable name to $burger
extends Node2D

@onready var burger: AnimatedSprite2D = $burger
@onready var grease_counter: Label = $greaseCounter
@onready var click_multipler_button: Button = $ClickMultiplerButton
@onready var click_sound_effect: AudioStreamPlayer2D = $ClickSoundEffect
1 Like

i just realized that the script your talking about is a duplicate oh my main game script so i just deleted it. Now the problem is fixed.