Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ThreeSpark |
Hi, I’m having some trouble instancing for some reason. Basically, what I’m doing is I have a variable that chooses a random number between 1 and 3, and then another variable that is a rounded version of the previous. If the second variable is 1, it will instance scene “x”, if it’s 2, it will instance scene “y”, and if it’s 3, it will instance scene “z”. Whenever I play the scene, and the second variable is 3, it gives an error saying “Invalid call. Nonexistent function ‘instance’ in base ‘PackedScene’.” Whenever the second variable is 1 or 2, nothing happens.
Here is the code:
extends Node2D
var island1 = load("res://procgen/nodes/island1.tscn")
var island2 = load("res://procgen/nodes/island2.tscn")
var island3 = load("res://procgen/nodes/island3.tscn")
var dir = 200
func _ready():
randomize()
var choice = rand_range(0, 4)
var rounded_choice = int(round(choice))
if rounded_choice > 3:
rounded_choice = 3
if rounded_choice < 1:
rounded_choice = 1
$Label.text = str(rounded_choice)
if rounded_choice == 1:
var i = island1.instance()
i.position = Vector2(position.x, position.y + dir)
get_parent().add_child(i)
get_parent().get_node("Player").position = i.position
if rounded_choice == 2:
var i = island2.instance()
i.position = Vector2(position.x, position.y + dir)
get_parent().add_child(i)
get_parent().get_node("Player").position = i.position
if rounded_choice == 3:
var i = island3.instace()
i.position = Vector2(position.x, position.y + dir)
get_parent().add_child(i)
get_parent().get_node("Player").position = i.position
anyone have a solution?