Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved [SOLVED] Object reference not set to an instance - using Instantiate().

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

  1. lars_unity743

    lars_unity743

    Joined:
    Aug 4, 2020
    Posts:
    4
    Thanks for reading!
    I am trying to instantiate a random object from a GameObject array.
    Whenever the Instantiation happens, I get an error saying:
    NullReferenceException: Object reference not set to an instance of an object


    The line im getting the error at, is this:
    Code (CSharp):
    1. void RoomInstantiation(){
    2.         instantiatedRoom = Instantiate(dungeonRoomPrefabs[assignedRoomNumber], transform.position, Quaternion.identity, gridObject.transform);
    3.     }
    at the start of the script i have assigned dungeonRoomPrefabs like this:
    public GameObject[] dungeonRoomPrefabs


    assignedRoomNumber is a random number between 0, and the length of dungeonRoomPrefabs.

    Why do I get an error? Please help me out here :(
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    gridObject may also be null
     
  3. lars_unity743

    lars_unity743

    Joined:
    Aug 4, 2020
    Posts:
    4
    I've checked, its not.
     
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Are all the dungeonRoomPrefabs elements assigned?

    PS
    In situations like this, when there is a couple of reasons for null reference exception in one line, split that line into several ones:
    Code (CSharp):
    1. var prefab = dungeonRoomPrefabs[assignedRoomNumber];
    2. var gridTransform = gridObject.transform;
    3. haveinstantiatedRoom = Instantiate(prefab, transform.position, Quaternion.identity, gridTransform);
    If the error is still at the Instantiate line, then prefab is null.

    PPS
    There's also a possibility that some field (dungeonRoomPrefabs or gridObject in your case) is assigned in the inspector, but when you start the scene it gets overwritten by your own code somewhere in Start or Awake or even some other scripts. So, the initial values get lost at the runtime and you get nulls or other values instead.
     
    Last edited: Aug 4, 2020
  5. lars_unity743

    lars_unity743

    Joined:
    Aug 4, 2020
    Posts:
    4
  6. lars_unity743

    lars_unity743

    Joined:
    Aug 4, 2020
    Posts:
    4
    Holy moly. I'm very embarrased right now. Turns out the gridObject was indeed null. Thank you so much for the help. It wasn't for nothing. This isn't the first time im getting the
    Object reference not set to an instance
    error.

    Next time, I'll make sure to put it into different lines to figure out which line causes the problem.