Search Unity

NullReferenceException: Object reference not set to an instance of an object in void Awake()

Discussion in 'Scripting' started by thisisorenji, Dec 19, 2019.

  1. thisisorenji

    thisisorenji

    Joined:
    Nov 17, 2019
    Posts:
    2
    In trying to deal with multiple scripts, I've found an odd situation where I can't access objects if they are placed into the Awake() function, but still can if they are placed into the Update() function.
    The code I am talking about is this:
    Code (CSharp):
    1. public class PlayerItems : MonoBehaviour
    2. {
    3.  
    4.     public GameObject playerObject;
    5.  
    6.     private Items itemData;
    7.  
    8.     public int selItem;
    9.  
    10.     private void Start()
    11.     {
    12.  
    13.         itemData = playerObject.GetComponent<Items>();
    14.  
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.  
    20.         Debug.Log("This item is called a" + itemData.GetName(selItem) + ". Its description reads \"" + itemData.GetDesc(selItem) + ".\"");
    21.  
    22.     }
    23.  
    24. }
    Accessing this script on the same object:
    Code (CSharp):
    1. public class Items : MonoBehaviour
    2. {
    3.  
    4.     public bool GetExists(int id)
    5.     {
    6.  
    7.         if (id > 000)
    8.         {
    9.  
    10.             return true;
    11.  
    12.         }
    13.         else
    14.         {
    15.  
    16.             return false;
    17.  
    18.         }
    19.  
    20.     }
    21.  
    22.     public string GetName(int id)
    23.     {
    24.  
    25.         if (id == 001)
    26.         {
    27.  
    28.             return "Sword";
    29.  
    30.         }
    31.         else
    32.         {
    33.  
    34.             Debug.LogError("Item ID Not Found");
    35.             return "Error";
    36.  
    37.         }
    38.  
    39.     }
    40.  
    41.     public string GetDesc(int id)
    42.     {
    43.  
    44.         if (id == 001)
    45.         {
    46.  
    47.             return "Weapon";
    48.  
    49.         }
    50.         else
    51.         {
    52.  
    53.             Debug.LogError("Item ID Not Found");
    54.             return "Error";
    55.  
    56.         }
    57.  
    58.     }
    59.  
    60. }
    The code works fine like this, but when I replace Update() with Awake(), it for some reason won't work, and outputs this error:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerItems.Awake () (at Assets/Scripts/PlayerItems.cs:25)
    Why does this happen? I know that there are other ways to ensure that it only happens for one frame, but I don't understand why it won't work on the first.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Awake() is called before Start(), so when you rename Update to Awake you're trying to use itemData before you give it a reference.

    Also, you shouldn't call methods on other scripts from Awake anyways, because there is no guarantee that the other script has finished initializing. You can use it for getting references though. Just don't use those references for anything in Awake.
     
    Molsergej083 and thisisorenji like this.
  3. thisisorenji

    thisisorenji

    Joined:
    Nov 17, 2019
    Posts:
    2
    Ah, thank you! For some reason, I thought that Awake was called on frame 1, instead of before start, so that's a huge mistake on my part.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This is helpful when you're unsure the order of events:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    thisisorenji likes this.