Setting transform position in Coroutines - not updating when expected?
Hey,
I have multiple coroutines that are ran to spawn objects. The code below would be ran by multiple coroutines calling their own prefabs to spawn.
e.g.
foreach coroutine
StartCoroutine(coroutine)
And then coroutine contents:
GameObject go = Instantiate(prefab);
go.transform.position = expectedPosition;
yield return null;
Using the above code; as each subsequent prefab is spawned they spawn at the prefabs default location before moving to 'expectedPosition'. The issue I would get is that the prefab being instanced has a final validation check to see if this 'expectedPosition' is empty.
The fix for this I found was to provide the position within instantiation as follows:
GameObject go = Instantiate(prefab, expectedPosition, new Quaternion());
yield return null;
Am I missing something really obvious as to why the first code example would cause an issue by the time a subsequent coroutine calls? The only thing I can imagine is multiple coroutines being ran on separate threads and the validation checks I run on the position at object instantiation would occur as each thread spawned the objects at the exact same time.