0

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);
}
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
  • Why don't you get the value of `PortalGameObject.transform.position ` you are storing there the value of m_PortalPosition before the WaitForSeconds ? – raven Mar 01 '16 at 16:34

2 Answers2

1

Make m_PortalPosition a local variable in the SpawnPortal coroutine, instead of making it a class variable. Pass it as a parameter to SpawnPlatform each time you call it and use the passed argument instead. Your code will change to:

private IEnumerator SpawnPortal()
{
    //...
    // Note the local variable?
    Vector3 m_PortalPosition = //...

    PortalGameObject.transform.position = m_PortalPosition;

    yield return new WaitForSeconds(1f);
    SpawnPlatform(m_PortalPosition);
}

private void SpawnPlatform(Vector3 PlatformPosition)
{
    //...
    // Commented out, not needed as we are using the argument
    //Vector3 PlatformPosition = m_PortalPosition;

    PlatformGameObject.transform.position = PlatformPosition;

    //...
}
EvilTak
  • 7,091
  • 27
  • 36
0

You need to use a local variable instead of a class field.

This way, each call to the function will have its own independent variable.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964