I am having difficulty trying to instance a scene with if statements. I am consistently getting this error no matter what I do.

Here is the code:

#Object Placement Variables signal place_cube const scroll_speed = 1.2 var holding_object = false var showing_menu = false #var the_object

export (PackedScene) var scene_1

func _ready(): #if holding_object == true: #pass

#This is for testing purposes
var cube_instance = scene_1.instance()#The error keeps showing up here
#which means that scene_1 is somehow "null"
cube_instance.set_translation(Vector3(0,0,8))
add_child(cube_instance)
pass

FYI: The variable scene_1 is set as a script variable for the node this script is connected to. It is a scene that contains a cube and the path to it is correct, as I have double checked it.

Your code should look like this

var cube_instance = preload("res://path to scene file").instance()
add_child(cube_instance)
cube_instance.set_translation(Vector3(0,0,8))

That will make it a new child of the object running the script. If you want to add it so it's not related to any other node...

var cube_instance = preload("res://path to scene file").instance()

var top_node = get_node("/root/your_top_node")
top_node.add_child(cube_instance)

cube_instance.set_translation(Vector3(0,0,8))

"base 'Nil'" indeed means your variable is holding nothing, so it's not being properly initialized to have something in it. Since you're exporting the variable (using export (PackedScene) var scene_1), I suppose you want to add it manually through the editor. Did you forget to do that by any change?

8 days later

Thanks for the replies, guys. I fixed the problem but completely forgot about this post I made :P . Hopefully this post will be useful to someone else.

5 years later