0

I'm making an endless runner game and I want a part of my code to wait for some seconds how can I do this? the problem is that it is deleting the terrain when the edge of the beginning of the new terrain hits the bottom of the camera view.

 void Start()
{
    StartCoroutine(Example());
}

IEnumerator Example()
{

    yield return new WaitForSeconds(5);

}
}

here is my code I want the recycle platform function to execute after a few seconds

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformManager : MonoBehaviour
{
[SerializeField]
private GameObject[] _platformPrefabs;
[SerializeField]
private int _zedOffset;


// Start is called before the first frame update
void Start()
{
    for (int i = 0; i < _platformPrefabs.Length; i++)
    {
        Instantiate(_platformPrefabs[i], new Vector3(0, 0, i * 4), Quaternion.Euler(0, 90, 0));
        _zedOffset += 4;
    }
}
// i want this to wait
public void RecyclePlatform(GameObject Platform)
{
    Platform.transform.position = new Vector3(0, 0, _zedOffset);
    _zedOffset += 4;

}



}

the recyclePlatform function need to wait before it executes

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Sindre Berge
  • 101
  • 5
  • 14
  • Not familiar with Unity, but does it support async/await with tasks, such as "await Task.Delay(5000);", to wait 5 seconds in RecyclePlatform method? – JJJulien Oct 29 '19 at 00:10
  • @JJulien Unity does support `async`, however this would cause the entire game thread to wait/freeze. Unity does however implement `routines` which create a separate thread, where we can perform these functions on. – TheLazyScripter Oct 29 '19 at 01:55
  • Note that UnityScript is a JavaScript derivative created for Unity, and is a completely separate language to C#. – ProgrammingLlama Oct 29 '19 at 04:19
  • Unity fully support async/await, but those are more for long file processing, or long computation as it can take advantage of multi-threads. When it comes to waiting for object to move, coroutines are more likely(tho await would do, don't get me wrong). – Everts Oct 29 '19 at 09:27
  • Possible duplicate of [How make the script wait/sleep in a simple way in unity](https://stackoverflow.com/q/30056471/1092820) – Ruzihm Oct 29 '19 at 13:38

4 Answers4

1

You can use this to wait whatever function or action you want to wait for. If you want to use Async Await:

public static async void DoActionAfterSecondsAsync(Action action, float seconds)
{
    await Task.Delay(TimeSpan.FromSeconds(seconds));
    action?.Invoke();
}

If you want to use Coroutine:

public IEnumerator void DoActionAfterSecondsRoutine(Action action, float seconds)
{
    yield return new WaitForSecondsRealtime(seconds);
    action?.Invoke();
}
zyonneo
  • 1,319
  • 5
  • 25
  • 63
1

Just put the code on a coroutine, and add the timeout

public void RecyclePlatform(GameObject Platform)
{
    StartCoroutine(RecyclePlatformCoroutine(Platform));
}

private IEnumerator RecyclePlatformCoroutine(GameObject Platform)
{
    yield return new WaitForSeconds(5);

    Platform.transform.position = new Vector3(0, 0, _zedOffset);
    _zedOffset += 4;
}
Innat3
  • 3,561
  • 2
  • 11
  • 29
0

Turn it into a Unity Coroutine.

private IEnumerator RecyclePlatform(GameObject Platform)
{
    yield return new WaitForSeconds(numSecondsToWait); # we can use floats to specify more accurate times
    Platform.transform.position = new Vector3(0, 0, _zedOffset);
    _zedOffset += 4;
}

We then start the routine like this whenever we want.

StartCoroutine(RecyclePlatform);
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19
0

I recommend you use a utility class for this. That would look something like this.

public static class CooldownManager
{
    private class HiddenMonobehaviour : MonoBehaviour { }

    private static readonly MonoBehaviour mono;

    static CooldownManager()
    {
        GameObject obj = new GameObject("CooldownManager");
        UnityEngine.Object.DontDestroyOnLoad(obj);
        mono = obj.AddComponent<HiddenMonobehaviour>();
    }

    public static Coroutine Cooldown(float cooldownDuration, Action action)
    {
        return mono.StartCoroutine(InternalCooldown(cooldownDuration, action));
    }

    private static IEnumerator InternalCooldown(float cooldownDuration, Action action)
    {
        yield return new WaitForSeconds(cooldownDuration);
        action.Invoke();
    }
}

Using this you can write:

public class Example : MonoBehaviour
{
    void Start()
    {
        Cooldown(2, () => RecyclePlatform(gameObject));
    }

    void RecyclePlatform(GameObject Platform)
    {
        Platform.transform.position = new Vector3(0, 0, _zedOffset);
        _zedOffset += 4;
    }
}