Hello! I’m trying to put a CharacterBody2D’s position inside a global variable script (which is inside of the parent Node2D) but some reason it couldn’t find it and gives me these two errors:
E 0:00:03:509 var_controller.gd:3 @ @implicit_ready(): Node not found: “Jotaro” (relative to “/root/VarController”).
E 0:00:03:509 _ready: Invalid access to property or key ‘position’ on a base object of type ‘null instance’.
And this is my script:
extends Node
@onready var jotaro: CharacterBody2D = $Jotaro
var player_turned = 1
var player_initial_pos: Vector2
func _ready():
player_initial_pos = jotaro.position
I tried a couple methods like get_tree().get_first_node_in_group() but it wouldn’t work either and Im not sure. And for more context this is how the scene is structured:
Player1 (Node2d) >>> Jotaro (CharacterBody2D, the only child of Player1)
Now that I realise no it doesn’t and I think I know what’s going on
The script is not fully attached to my Player1 node2D, this VarController node used to be inside a scene but it might have been moved to root after I deleted it. What should I do though? Write a whole new script for Player1?
If you made the script autoloaded it will always be attached to a node that “outside of scene”. You can let Jotaro register itself to the autoloaded script, or use a correct path from autoloaded node to Jotaro if you can guarantee that the Jotaro will always be at that path. I’d go with the former.