Problems with instance when using a different scene.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ArAdev
:warning: Old Version Published before Godot 3 was released.

Hello, Im new with Godot, and I`m having a problem with the Gdscript.
The problem is that Im trying to do a gun that shoots a bullet, and it aparently works except that when I instance the gun on my world scene it doesnt work as intended, and the bullet spawn in a different place inside the world(other than in the Position2D node that I made as spawn point for the bullet)
The gun works as intended in its own scene but when I instance the gun scene in the world scene it has the problems that I just said.
Im using Godot Engine 2.1.4.

Here is the code for the gun:

func spawn_bullet():
var bullet= bullet_scene.instance()
var pos= get_node("Pos_bullet").get_global_pos() 
bullet.set_pos(pos)
get_parent().add_child(bullet)

Thanks in anticipation, Im having so much trouble with this.

Did you used preload function to load bullet tscn

Something like this

onready var bullet_scene = preload(“res://bullet.tscn”)

Then make instance for that scene, then add it to scene tree then set pos normally

xCode Soul | 2017-11-06 19:42

Thanks a lot xCode Soul, I realized that the problem was that the script of the gun was attached to the sprite of the gun, and the parent on the gun scene was a node2D ,and aparently it was causing problems, so I changed it and it now it works.

ArAdev | 2017-11-06 20:46