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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Null check in coroutine

Discussion in 'Scripting' started by igaguri132, Apr 26, 2023.

  1. igaguri132

    igaguri132

    Joined:
    Apr 20, 2023
    Posts:
    5
    I have this coroutine for blinking a sprite, but it contains redundant try-catch sections. How can I simplify it?
    Using "if (sprite != null)" resulted in a null pointer exception if the object was destroyed during the null check and value assignment.
    Code (CSharp):
    1.  
    2.     private static IEnumerator BlinkCoroutine(SpriteRenderer sprite, float frequency, bool[] flag)
    3.     {
    4.         while (flag[0])
    5.         {
    6.             try
    7.             {
    8.                 sprite.enabled = !sprite.enabled;
    9.             }
    10.             catch (NullReferenceException)
    11.             {
    12.                 yield break;
    13.             }
    14.             yield return new WaitForSeconds(frequency);
    15.         }
    16.  
    17.         try
    18.         {
    19.             sprite.enabled = true;
    20.         }
    21.         catch (NullReferenceException)
    22.         {
    23.             yield break;
    24.         }
    25.     }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    You can not destroy an object during the null check or during an assignment. You can only work with GameObjects and components on the main thread and coroutines only run on the main thread. So if you do a null check right before assigning a value to the object, it can not magically turn to a fake null object in between.
     
  3. igaguri132

    igaguri132

    Joined:
    Apr 20, 2023
    Posts:
    5
    Sorry, I mistook the name of the error, actual error was 'MissingReferenceException'.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Can't you just ...?
    Code (CSharp):
    1. private static IEnumerator BlinkCoroutine(SpriteRenderer sprite, float frequency, bool[] flag) {
    2.   if(flag[0]) {
    3.     if(sprite) sprite.enabled = !sprite.enabled;
    4.     yield return new WaitForSeconds(frequency);
    5.   }
    6. }
    You need to stop the coroutine elsewhere if that's a concern, but it shouldn't crash.

    The biggest complication about this is trying to squeeze everything through coroutines.
    They are not your paracetamol. Heck not even paracetamol is paracetamol. It's actually highly toxic.

    If I were you, I would build a dedicated time measuring thing and hook events to it, making sure I have ample room for growth, sorting weird edge-cases, and access to features like being able to mute or isolate this behavior this to help me test things in the long run etc.
     
    Last edited: Apr 26, 2023
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Why are you writing code to blink something? Have you considered using an animation?
     
    orionsyndrome likes this.
  6. igaguri132

    igaguri132

    Joined:
    Apr 20, 2023
    Posts:
    5
    Thank you for the suggestion. I remade it with animation and it seems to work.
     

    Attached Files:

    orionsyndrome and Kurt-Dekker like this.