Initial targetPosition for Unit

Setting the targetPosition in Awake() works to stop a unit from constantly moving to 0,0,0, but this only works if the unit is added before the game is running. When I add a unit while the game is running, they like to run to 0,0,0 first. Could the setting of targetPosition be done within Update() as well, or would that create other problems?

Are you instantiating the Unit on 0,0 and only after the Instantiate(); changing the position? If so yes it will do that since you call Instantiate(); the Awake runs and sends the unit to 0,0 and only after that you change the transform.position
If that’s the case the solution is to use the Instantiate(); version that takes in a position

1 Like

I’m not quite sure what Instantiate() is. I’m not calling it myself, so is that something that’s running automatically?

You said you were adding a unit after the game is running, how are you doing that?

I drag a Unit prefab onto GameScene while the game runs

Hmmm… something we hadn’t really considered as it’s not something that would happen in a packaged game. You’re dragging it from the Assets folder into the scene as it’s running, and things may not work entirely as expected…
It is surprising that Awake() isn’t called when the item is dragged into the scene, although it’s possible that Awake() is called when the drag begins and the transform isn’t set until the drag ends… I’m not sure what the solution would be in this case…

1 Like

Sounds like maybe when you drag a game object Unity calls Instantiate(); and then repositions the object. I never tried that since it’s not something you can do in the final game so it’s not an issue.
If you want to spawn a Unit after the game starts use Instantiate(); and use the version that sets the position right away, don’t drag logic objects after the game is running.

2 Likes

Sounds good. Thanks for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms