Why I can't create 2 instances

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

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