- Home /
Object created through Instantiate does not update its position at the first frame
Hey all.
I have a small problem:
SomeObject o = Instantiate(object_prefab, spawnPosition, Quaternion.identity) as SomeObject;
It creates the object as supposed, but the problem is - it's position is updated at the next frame. Basically the object is created at (0,0) position and in the next frame it is at "spawnPosition".
I had this problem in the past many times with different scripts and also it does't update rotation on first frame. Very frustrating. How to prevent it?
Answer by fafase · Jan 09, 2017 at 06:43 AM
You could explicitly set the position:
GameObject o = Instantiate(object_prefab) as GameObject;
o.transform.position = spawnPosition;
o.transform.rotation = Quaternion.identity; // Not so useful since already set by default
Hey, of course I tried it before. It causes the same issue. The object will be at prefab's default position for just one frame and in the next frame will appear at the expected position.
Just a wild guess, have you tried setting that in LateUpdate()?
Thanks for your suggestion. I haven't and I'd rather avoid that. I am relying too much on FixedUpdate() and moving to LateUpdate() would be very painful.
your update might be running faster than your fixed update.
This might work, as it will set it to inactive then change postions and what not then set it as active. At the very least, it shouldn't be visible... I also had a similar problem.
GameObject o = Instantiate(object_prefab) as GameObject;
o.SetActive(false);
o.transform.position = spawnPosition;
o.transform.rotation = Quaternion.identity; // Not so useful since already set by default
o.SetActive(true);
Hey. I also tried this. I also turned off SpriteRenderer in a prefab, Instantiated the object, set it's position and then turn on SpriteRenderer and still have the problem.
Are you sure spawnPosition isn't (0,0)????? What is its value before the instantiate. The prefab object, what is its start, awake, onenable methods doing? What is its update method doing.
SomeObject o = Instantiate(object_prefab, spawnPosition, Quaternion.identity) as SomeObject;
That line of code should work as is and put it in the right spot unless the spawnPosition isn't correct or maybe you are adding it as a parent to something and that's limiting it's movement?
this must be correct if called in Awake, Start or any Update. The moment it's called, it's there. If not something else is wrong.
setting the gameobject to any state is not necessary
Your answer
Follow this Question
Related Questions
Projectiles trigger affecting other projectiles of same prefab - C# 0 Answers
Animation doesn't affect the new instantiated object. 0 Answers
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Instanciate prefab responsively 1 Answer
Show GUI Texture on Trigger and Instantiate Object if Button pressed while In Trigger? 1 Answer