Struggling to identify scene relative to root node

Godot Version

Godot_v4.7.2

Question

Hello! I’m trying to write a global script that will handle transitioning between scenes in my game.

My issue currently is that I can’t get the script to identify the scene I want disabled (the “Shop” scene.) I don’t know if it’s syntax I don’t know yet or if I’m missing something. I’ve tried making an onready variable for the scene and referencing it any way I can but it always says the node is “not declared in current scope.”

extends Node

@onready var shop: Control = $Shop

# Scene Control Var 
var pet_scene_now : bool = false
var shop_scene_now : bool = false

# Scene Control Code 
# Enable game start, first instance of petting scene.
func trans_pet_scene():
	get_tree().change_scene_to_file("res://scenes/main.tscn")
	pet_scene_now = true
	get_node("Shop").set_process_internal(false)

Is this global script attached to any node in your scene?

The problem could be that you use @onready in your global scripts. Your global script’s _ready() likely runs before the node you want it to access has loaded in. Instead, use the _ready() in the scene you want the global to reference and make it assign itself when it loads in.

So global has var shop: Control

The shop scene has global.shop = self and you only run it in _ready()

Oh, yes, the global script is “game_manager” and it’s attached to the root node “Main”.

Global scripts shouldn’t be attached to scene nodes. If a script is attached to a scene node it shouldn’t be set up as a global autoload. It’s either-or.

I just found a tutorial that does what I need.