- Home /
Unity does not update fast enough
I have the following code in C#:
transform.position = newMarblePos;
Collider[] marbles = Physics.OverlapSphere(newMarblePos, 0.1f);
Debug.Log (marbles.Length ) ;
This prints out 0. This does not make any sense: clearly the GameObject should show up in the intersection. Why is this the case? Could it be that by the time of my Debug.Log statement, Unity has not updated the transform yet? Does Unity not wait for a statement to finish before executing the next one? Regardless, any insight into the cause of this bug and how to work around it would be greatly appreciated.
The position is updated immediately. Do the transform have a collider attached?
Answer by Baste · Nov 19, 2014 at 09:45 AM
The position is updated immediately. The overlapsphere is a part of the physics engine, though, and the physics engine updates in the FixedUpdate step. So the overlapsphere won't find the original marbe before at least one FixedUpdate has run.
If you want to guarantee that the overlapsphere finds the original marble, put everything in a coroutine, and use WaitForFixedUpdate to wait until the colliders has updated.
Thanks Baste, I think I'll try that. I've verified that the marble's transform is correctly updated eventually, but not by the time I need to use it.
Your answer
Follow this Question
Related Questions
physics.OverlapSphere and loops troubles 0 Answers
Weird execution order when instaniating a gameObject with too many children 1 Answer
Canvas Rendering Execution Order Question 0 Answers
Mob AI not immediately finding object it is looking for, and crashing when it eats food? 1 Answer
Script Execution Order Question 1 Answer