"Invalid call. Nonexistant function 'instance' in base 'bool'" but the scene I'm trying to instance isn't a bool

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LeipeMeuteLeider

Hi y’all, I’ve been stuck on this seemingly simple issue for an embarrassingly long time now so I’m finally gonna give in and just ask: what the hell does this error mean and how do I fix it? I’m trying to make a basic bullet script for a 2d game, but when I try to instance it I get this error every time. I’m absolutely sure that the path to the scene is correct, and I’m also absolutely sure that the variable doesn’t get overwritten as a bool since this is a new variable that I haven’t used for anything else. See the code below:

   var bullet = preload("res://Bullet.tscn")

    func _process(delta):

    func HeldItemLoop():
    	if Input.is_action_pressed("player_left_click"):
	        	var bullet = bullet.instance() #<=== this is where the error occurs
	        	bullet.position = get_global_position()
	        	bullet.rotation = get_angle_to(get_global_mouse_position())

What am I doing wrong here?

try to use different variable names for preloaded scene and intanced scene, like

var bulletinst = bullet.instance()

Inces | 2022-04-25 20:56

Exactly the answer I needed, thanks!

LeipeMeuteLeider | 2022-04-25 22:36

Hmmm… I’d expect that error to be:

Invalid call. Nonexistant function 'instance' in base 'Nil'

(note Nil instead of bool).

That’s because you’ve defined two versions of bullet. The first one contains the preloaded scene and is global- defined here:

var bullet = preload("res://Bullet.tscn")

The second one is a variable that’s local to the HeldItemLoop() method - defined here:

var bullet = bullet.instance()

So, in that case, the bullet.instance() reference would be looking at the local variable, which hasn’t been defined to anything yet - hence I’d expect the error to contain Nil instead of bool.

Either that, or there’s more code interaction than shown above…

jgodfrey | 2022-04-26 03:27