Search Unity

Fix now script problem with stopped coroutine by disabled GameObject

Discussion in 'Scripting' started by florinel2102, Oct 27, 2020.

  1. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Hi guys , I want to share with you a 'system for coroutines'/ interesting technique that will help you to stop wondering if the GameObject will be disabled . So if the game is paused (and main canvas will be disabled) the coroutine continue to work .
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. using UnityEngine.Events;
    6.  
    7. /// <summary>
    8. /// Instance used for coroutine on deactivated object
    9. /// </summary>
    10. public class ObjectEff : MonoBehaviour
    11. {
    12.     public static ObjectEff instance;
    13.  
    14.  
    15.    void Awake()
    16.    {
    17.         MakeInstance();
    18.    }
    19.     public void MakeInstance()
    20.     {
    21.         instance = this;
    22.     }
    23.  
    24.     /// <summary>
    25.     /// Execute a coroutine especially when your gameobject could be inactive
    26.     /// </summary>
    27.     /// <param name="coroutine">Make an instance of IEnumerator and send as a parameter for coroutine</param>
    28.     public void ExecuteCoroutine(IEnumerator coroutine)
    29.     {
    30.         StartCoroutine(coroutine);
    31.     }
    32.  
    33. }
    34.  
    Put this script on a single GameObject in the scene (also you can set DontDestroyOnLoad )

    Basically you just have to call instead of StartCoroutine(coroutine) -> ObjectEff.instance.ExecuteCoroutine(coroutine)

    Good luck with using this system :) !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This is broadly called a coroutine scheduler or coroutine runner.

    Beware that with the above code, if this ObjectEff hosting object persists beyond the duration of the objects hosting data related the coroutines, you could start accessing objects that have been destroyed and are non longer available.

    Also, note that the above does not truly implement a singleton, just a lazy "latest instance" referencer, of which there can only be one. It is also reliant on it being manually singleton-enforced rather than being emergently enforced.

    Here are some super-simple Singleton examples to take and modify if you want to make the above code more general-purpose:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
     
    florinel2102 likes this.
  3. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Hi Kurt , you're right with singleton since this script doesn't take any reference or data and I don't even think to 'bad side' of this system - but I guess the system can be fixed so will automatically stop coroutine (when gameobject is destroyed ) after that other miss reference should be resolved inside of coroutine .