I'm trying to instantiate object(scene) and set it's spawn position at the spawn time. Here's the code I wrote:

func spawn_asteroid():
    var spawn_position: Vector3 = get_spawn_position()

    var asteroid_instance = asteroid_scene.instance()
    asteroid_instance.transform.origin = spawn_position
    spawned_asteroids.append(asteroid_instance)

    add_child(asteroid_instance)

asteroid_scene is PackedScene, so get instance first, and then update it's position, and finally use add_child to append current scene's tree.

It seems fine to me, however when I run the game, the object was spawned at (0,0,0) and then move spawn_position.

I changed it's position before it appended to scene, but why it still (0,0,0) and right after move to the specified position?

In Unity, change object's location right after instancing works. In Godot Engine, is this approach invalid?

I think you can change the transform after calling add_child, if I remember correctly.

From what I have gathered looking online, it looks like your code should work though. Are you checking the position in _ready on the spawned asteroid? Also, are you sure that spawn_position is not (0, 0, 0)?

Maybe try the following code, though I'm not sure if it will work not:

func spawn_asteroid():
	var spawn_position : Vector3 = get_spawn_position()
	var asteroid_instance = asteroid_scene.instance()
	spawned_asteroids.append(asteroid_instance)
	
	add_child(asteroid_instance)
	asteroid_instance.transform.origin = spawn_position
	
	# Something you can try is move the code in _ready on your asteroid to a function
	# called _setup, and then use the following line of code:
	# asteroid_instance._setup()

The code above should change the position of the asteroid to the position in spawn_position, though if you are checking or changing the asteroid position in the _ready function of the asteroid script, you might need to make adjustments.

That is strange that the position is not being updated. I remember I have had this issue before, but I think I just worked around it by using an additional function.

@TwistedTwigleg spawn_position is not (0,0,0) because it moved right after it was 0,0,0 and there's no changing location stuffs, also no physics stuffs happen even there is something in (0,0,0).

Try changing transform after add_child doesn't work either. Seems like it's glitch of 3.1.1: https://github.com/godotengine/godot/issues/23340 https://github.com/godotengine/godot/issues/22904 https://github.com/godotengine/godot/issues/33435

3 years later