Declared PackedScene Returning as Null

Godot Version

4.5

Question

I am making a game and in this game, I am trying to instantiate an object when another object reaches a certain height. I instantiate that scene in other parts of the game and it works fine there, however, when I try and instantiate the scene in this particular scenario it doesn’t work.

I have replaced the scene path in the preload and it hasn’t changed anything, I am very sure I haven’t misspelled anything, and I have changed where the preload is declared and nothing has worked. The errors I am getting are:

“Failed to instantiate scene state of ’ ', node count is 0. Make sure the PackedScene resource is valid.”

“Invalid assignment of property or key ‘position’ with value type of ‘Vector3’ on a base object of type ‘null instance’.”

This is where the preload for the instanced node is declared:

var green_rock_scene: PackedScene = preload("res://scenes/Hull/green_rock.tscn")

This is where I instantiate the green rock scene in the process function:

if global_position.y > 0.5 and made_new_rock == false:
	var new_green_rock = green_rock_scene.instantiate()
	new_green_rock.position = Vector3(1.765, global_position.y - 2.331, 0.0)
	new_green_rock.rotation_degrees.y = -90
	sub_viewport.add_child(new_green_rock)
	made_new_rock = true

When I print the PackedScene in the ready function, it returns “<PackedScene#-9223371994358086168>”

But when I try printing the new_green_rock variable after it’s declared, it returns, “<Object#null>”

I really have no idea what would be causing these errors, I use literally the exact same scene path in another node for instantiating that scene and it works fine.

Use load instead of preload. Preferably you would only store the string and load just before it’s needed.

preload can create cyclical dependencies which are silently resolved by failing to load the resource and thus null. You should only use preload for very small, self-contained resources.

This fixed the issue, thanks for helping