This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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):
        HeldItemLoop()

    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())
                get_parent().add_child(bullet)

What am I doing wrong here?

Godot version 3.3.3
in Engine by (18 points)

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

var bulletinst = bullet.instance()

Exactly the answer I needed, thanks!

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...

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.