Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making an always running/active game.

Discussion in 'General Discussion' started by Vecna, Jan 24, 2023.

  1. Vecna

    Vecna

    Joined:
    Apr 2, 2014
    Posts:
    31
    I am creating a game that needs to run 24/7. I am doing an API call every 5 seconds to check for a change in game state however the scripts stops running after some time. I have created a timer in Update() and that stops after an hour or so and I have created a Coroutine that calls itself after 5 seconds and that stops after 3 or so hours. The rest of the game is running fine. Is the script falling asleep? Does anyone have an idea what is going on?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,753
    How can we tell if you won't actually show us the code in question?
     
  3. Vecna

    Vecna

    Joined:
    Apr 2, 2014
    Posts:
    31
    Here is the Update code (Runs for about an hour)
    Code (CSharp):
    1.    void Update()
    2. {
    3.     if (Time.time > callAPIDuration_PlusTime)
    4.     {
    5.         DoAPICall();
    6.         callAPIDuration_PlusTime = Time.time + 5;
    7.     }
    8. }
    Here is the Coroutine (Runs for 3 hours then stops)

    Code (CSharp):
    1.  IEnumerator CallApi()
    2.     {
    3.         yield return new WaitForSeconds(5f);
    4.         DoAPICall();
    5.         StartCoroutine(CallApi());
    6.     }
     
    Last edited: Jan 24, 2023
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,048
    You reschedule a new coroutine every time. It would be more efficient if you put the yield and call in a while(true) statement.