Search Unity

Question Animation not working in Coroutine, NullReferenceException

Discussion in 'Scripting' started by CodeMateo, Aug 4, 2020.

  1. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Yeah so it says
    NullReferenceException: Object reference not set to an instance of an object
    CharacterSpawner+<Knight>d__5.MoveNext () (at Assets/Code/CharacterSpawner.cs:36)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <23a7799da2e941b88c6db790c607d655>:0)
    UnityEngine.MonoBehaviour:StartCoroutine(String)
    CharacterSpawner:Start() (at Assets/Code/CharacterSpawner.cs:27)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CharacterSpawner : MonoBehaviour
    7. {
    8.     public GameObject characterKnight;
    9.     public GameObject characterWizard;
    10.     public bool cameraTarget;
    11.  
    12.     private InventoryUI inventory;
    13.  
    14.     void Start()
    15.     {
    16.         if (CharacterSelection.knight == true)
    17.         {
    18.             StartCoroutine("Knight");
    19.         }
    20.         if (CharacterSelection.wizard == true)
    21.         {
    22.             StartCoroutine("Wizard");
    23.         }
    24.  
    25.         if (CharacterSelection.knight == false && CharacterSelection.wizard == false)
    26.         {
    27.             StartCoroutine("Knight");
    28.         }
    29.         inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<InventoryUI>();
    30.     }
    31.  
    32.     IEnumerator Knight()
    33.     {
    34.         Instantiate(characterKnight, transform.position, characterKnight.transform.rotation);
    35.         cameraTarget = true;
    36.         inventory.character.SetBool("Knight1", true);
    37.         yield return new WaitForSeconds(4f);
    38.         Destroy(this.gameObject);
    39.     }
    40.  
    41.     IEnumerator Wizard()
    42.     {
    43.         Instantiate(characterWizard, transform.position, characterWizard.transform.rotation);
    44.         inventory.character.SetBool("Wizard1", true);
    45.         cameraTarget = true;
    46.         yield return new WaitForSeconds(4f);
    47.         Destroy(this.gameObject);
    48.     }
    49. }
    Am I not able to animate in a Coroutine?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Null exceptions has nothing to do with coroutines, it means something has no value.
    In this case, your inventory has no value. Why? Look at your code. The first time Start is called, it hits your third if statement which triggers your coroutine. When it goes into your coroutine, you tell it to access inventory. At this point inventory is null. It's not until after the if statement that you assign a value to inventory. Move that line to the top of Start and you should be fine.
     
    CodeMateo likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.
     
    CodeMateo likes this.
  4. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    omg thank you that worked perfectly, shows how I'm such an amateur lol