0 votes

I have two projectiles, bullet and rocket. when I click left mouse bullet system is working fine but when i click right mouse, I get "add_child: Can't add child 'Rocket' to 'root', already has a parent 'root'." Both of them are the same code. I don't understand why my rocket system is not working.

Here is my code:

onready var guns = [
    $MuzzleRight,
    $MuzzleLeft
]

onready var rockets = [
    $LauncherRight,
    $LauncherLeft
]


export(Resource) var bullet_res
onready var bullet_scn : PackedScene = load(bullet_res.resource_path)

export(Resource) var rocket_res
onready var rocket_scn : PackedScene = load(rocket_res.resource_path)


func _ready():
    bullet_scn = load(bullet_res.resource_path)
    rocket_scn = load(rocket_res.resource_path)


func _physics_process(delta):

    process_fire()
    process_rocket()


func process_fire():
    if Input.is_action_just_pressed("fire"):
        var bullet_group = [bullet_scn.instance(), bullet_scn.instance()]
        var i = 0

        for b in bullet_group:
            b.transform = guns[i].global_transform
            get_tree().root.add_child(b)
            b.fire() # function from Bullet.gd
            i += 1


func process_rocket():
    if Input.is_action_just_pressed("rocket"):
        var rocket_group = [rocket_scn.instance(), rocket_scn.instance()]
        var j = 0

        for r in rocket_group:
            r.transform = rockets[j].global_transform
            get_tree().root.add_child(r)
            print(get_tree().root.add_child(r))
            r.fire_rocket() # function from Rocket.gd
            j += 1
Godot version 3.3.2
in Engine by (12 points)

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 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 webmaster@godotengine.org with your username.