I'm trying to spawn a platform where the portal position was just at, so it looks like it's coming out of a portal.
I'm having trouble with this line: Vector3 PlatformPosition = m_PortalPosition;
, because SpawnPortal
waits for 1 second and then calls the spawn platforms method, the m_PortalPosition
gets overwritten by the time it gets to assigning the PlatformPosition
.
How do I 'remember' the variables of the m_PortalPosition
before the WaitForSeconds(1f);
was called?
void Awake()
{
for (int i = 0; i < 10; i++)
{
StartCoroutine(SpawnPortal());
}
}
private IEnumerator SpawnPortal()
{
// Do portal stuff
// Get an Platform from the object pool.
GameObject PortalGameObject = m_PortalObjectPool.GetGameObjectFromPool();
// Generate a position at a distance forward from the camera within a random sphere and put the Platform at that position.
m_PortalPosition = m_Cam.position + Vector3.forward * m_SpawnZoneDistance + new Vector3(UnityEngine.Random.insideUnitSphere.x * m_PlatFormZoneRadius.x, UnityEngine.Random.insideUnitSphere.y * m_PlatFormZoneRadius.y, UnityEngine.Random.insideUnitSphere.z * m_PlatFormZoneRadius.z);
PortalGameObject.transform.position = m_PortalPosition;
yield return new WaitForSeconds(1f);
SpawnPlatform();
}
private void SpawnPlatform()
{
// Get an Platform from the object pool.
GameObject PlatformGameObject = m_PlatformObjectPool.GetGameObjectFromPool();
//Set the platform position to the portal position, problem with this line
Vector3 PlatformPosition = m_PortalPosition;
PlatformGameObject.transform.position = PlatformPosition;
// Get the Platform component and add it to the collection.
Platform Platform = PlatformGameObject.GetComponent<Platform>();
m_platforms.Add(Platform);
// Subscribe to the Platforms events.
Platform.OnPlatformRemovalDistance += HandlePlatformRemoval;
m_lowestPlatformPositionY = m_platforms.Min(x => x.transform.position.y);
}