Problem with getting main node reference

Godot Version

4.41

Question

I keeping getting this error when trying to retrieve a reference to my top Node in the tree.

func GetRootNode():
return get_tree().get_current_scene()

and the error:
Cannot call method ‘get_current_scene’ on a null value.

I certainly have a top node, I created int he editor = New 3D Scene.

Are you using this function directly after using change_scene_to_file or packed?

No, I’m using it top most of the start of the program. heres my first few lines of code.

extends Node3D

func _init():

var theroot = sg.GetRootNode()

# Create camera
var camera = Camera3D.new()
camera.position = Vector3(0, 0, 5)
camera.look_at(Vector3.ZERO, Vector3.UP)
theroot.add_child(camera)

_init is before the node is in the scene tree, so it cannot get_tree() as it isn’t in one. You may have to move this to _ready

2 Likes

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