Search Unity

Starting/Stopping Coroutines breaking game objects?

Discussion in 'Scripting' started by CurranLab, May 26, 2015.

  1. CurranLab

    CurranLab

    Joined:
    May 26, 2015
    Posts:
    2
    I'm making a task that runs within a coroutine loop. If the player runs out of time, I stop the coroutine, reset some variables (but keep others such as score) and start the coroutine again. After this time-out happens a couple of times the player object stops working. It has it's own controller for player object movement and properties, but when I press the arrow keys it doesn't move.

    Is it bad to potentially start and stop the main coroutine so often? I don't want to reload the entire level because I need to keep track of how many times they've gone through the main coroutine, their score, what game objects from the pool have been used, etc. Would it be better to store everything in PlayerPrefs and reload the entire scene? Is it just the method I'm using to start and stop the coroutines?

    My method so far:
    In the game controller, I have a timer running in Update. When the timer runs out it stops the main coroutine and calls the TimeOut function I made (I removed some of the details of what I'm resetting to save space):

    Code (CSharp):
    1. public void TimeOut ()
    2.     {
    3.         timeOutText.text = "Time's up!";
    4.         /* Play time out beep */
    5.         /* Reset all currently active bugs to inactive */
    6.         /* Reset example bug and it's properties */
    7.         /* Reset feedback & timer properties */
    8.         blocks +=1;
    9.         StartCoroutine(BlockOver());  // I read that this method is better for calling coroutines, so I use this when I don't need to stop them
    10.     }
    But that can't restart the coroutine because it's not an IEnumerator, so I then have it call an IEnumerator BlockOver() to start the coroutine again:

    Code (CSharp):
    1. IEnumerator BlockOver ()
    2.     {
    3.         /* Wait briefly between blocks before making next example bug */
    4.         yield return halfBlockWFS;     // I created variables for WaitForSeconds at the start and reuse them as needed
    5.         /* Start main block coroutine over again */
    6.         StartCoroutine ("SpawnWaves");  // I use the string to start the coroutine because this is the one I keep starting/stopping and I think you need the string version to use StopCoroutine
    7.     }
    Any help would be appreciated. I've been reading a lot of Unity and C# forums/documentation, but couldn't find anything specific to the consequences of starting/stopping game controller coroutines frequently on other game objects.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @CurranLab, You are much better off Starting/Stopping Coroutines than saving to PlayerPrefs and completely reloading your scene. That sounds nuts! It's perfectly fine to Start/Stop them as much as you need.

    You can store your coroutine in a IEnumerator variable, look at the documentation:
    http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html

    Or you need to Start the Coroutine using a string. (And you stop it the same way).
    Unless you want to pass some arguments, I will usually tend to use the latter
     
    L-Tyrosine likes this.
  3. CurranLab

    CurranLab

    Joined:
    May 26, 2015
    Posts:
    2
    Thank you for replying so quickly, @sluice ! I'm glad starting/stopping coroutines is the recommended route, I really didn't want to restructure the game to constantly reload the scene. I ended up removing the wait from within BlockOver(), so I'm able to call "SpawnWaves" from within TimeOut() directly and deleted the extra BlockOver() function.

    Unfortunately my game objects are still getting "stuck". However it seems to be happening more now in general after adding more of them, not necessarily related to yields and coroutine starting/stopping.