Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Calling GameObject from another script returning error.

Discussion in 'Scripting' started by Topfall, Nov 15, 2023.

  1. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    6
    I have a script that returns a game object that is active from a list. When calling the game object from another script it returns an Error(NullReferenceException: Object reference not set to an instance of an object), but when calling it from the same script it returns a game object fine.

    First script:
    Code (CSharp):
    1. public GameObject GetHand()
    2. {
    3.     GameObject activeObject;
    4.     foreach (GameObject obj in Objects)
    5.     {
    6.         if (obj.activeSelf)
    7.         {
    8.             activeObject = obj;
    9.             if (activeObject != Objects[0])
    10.             {
    11.                 return activeObject;
    12.             }
    13.         }
    14.     }
    15.     Debug.Log("NULL");
    16.     return null;
    17. }
    Second script calling the game object:
    Code (CSharp):
    1.  if (inv.GetHand().name != null)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,851
    You forgot some of the most important information about your problem - you didn't include the full error message which includes the filename and line number where the error is occurring.
     
  3. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    6
    The error is from the second script:
    NullReferenceException: Object reference not set to an instance of an object
    Interaction.CheckHand (System.String type) (at Assets/Scripts/Player/Interaction.cs:47)
    Interaction.CheckInteraction () (at Assets/Scripts/Player/Interaction.cs:37)
    Interaction.Update () (at Assets/Scripts/Player/Interaction.cs:16)

    (part of second script- line 47)
    Code (CSharp):
    1. if (inv.GetHand() != null)
    (part of second script- line 37)
    Code (CSharp):
    1. if (CheckHand("Key"))
    2. {
    3.     Debug.Log("Key");
    4. }
    (part of second script- line 16)
    Code (CSharp):
    1. CheckInteraction();
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,851
    You need to show which line of code is Assets/Scripts/Player/Interaction.cs:47
     
  5. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    6
    (part of second script- line 47) in void CheckHand()
    Code (CSharp):
    1. if (inv.GetHand() != null)
    (part of second script- line 37) in void CheckInteraction()
    Code (CSharp):
    1. if (CheckHand("Key"))
    2. {
    3.     Debug.Log("Key");
    4. }
    (part of second script- line 16) in void Update()
    Code (CSharp):
    1. CheckInteraction();
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,851
    If that line is
    if (inv.GetHand().name != null)
    , then there are two possiblities:
    inv itself is null
    inv.GetHand() is returning null.

    You need to figure out which is true. You can start by adding some log statements:

    Code (CSharp):
    1. if (inv == null) {
    2.   Debug.Log("Inv was null!");
    3. }
    4. else if (inv.GetHand() == null) {
    5.   Debug.Log("GetHand() returned null!");
    6. }
    Once we identify which thing is null we can start to fix it.
     
  7. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    6
    Thanks fixed, inv was set to a wrong object
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,332
    Glad you got it sorted.

    For future reference, 100% of all NullReferenceExceptions in Script land are fixed by this process:

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that