Search Unity

NullReferenceException Error when try to call a function in another GameObjects Script

Discussion in 'Getting Started' started by poccix, Dec 11, 2017.

  1. poccix

    poccix

    Joined:
    Dec 5, 2017
    Posts:
    2
    Hello there, after hours of trying out everything i found about that topic i'll start a thread because i wont get it to work.

    I have two Scripts.
    One script is attached to an GameObject.
    That GameObject has other GameObjects as children, so the first GameObject acts like a container for them.
    The children GameObject has also a script attached, with a public variable that i need to get into the Script attached to the container.

    I use:

    Code (CSharp):
    1. Transform[] childTransform = nodes.GetComponentsInChildren<Transform>();
    2. foreach (Transform child in childTransform) {
    3.         childObj.Add(child.gameObject);
    4. }
    To store my child GameObjects in a List - that works.

    Then i have a for loop with the problem:

    Code (CSharp):
    1.  
    2. for (int i = 1; i < childObj.Count; i++) {
    3.    // Working part, so the GameObject in List must be there because i get the correct positions
    4.    vector = string.Format("\"vector\" : {{\"x\" : {0}, \"y\" : {1}, \"z\" : {2}}}", childObj[i].transform.position.x, childObj[i].transform.position.y, childObj[i].transform.position.z);
    5.  
    6.    // Not working part, because my objScript variable is "null"
    7.    NodeScript objScript = childObj[i].GetComponent<NodeScript>();
    8.    if(objScript == null) {
    9.        Debug.Log("is null???"); //RETURNS ! So it is null, but why?
    10.    }
    11. }
    12.  
    And yes the GameObjects have the script "NodeScript" attached to them in Inspector...

    Hope someone can help out.

    Best regards,
    poccix
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    If I'm not mistaken i think GetComponentsInChildren also returns the component on the root object as well. So unless you also have a NodeScript on your root object you'll get a null exception on the first time through the childObj list as the object in the childObj[0] position will be the root object (container object).

    Add an

    Code (csharp):
    1.  
    2. if( child.gameObject != gameObject )
    3.  
    before adding to your childObj list.


    On a side node, can I ask why you don't just call GetComponentsInChildren<NodeScript>() instead of GetComponentsInChildren<Transform>()? That would remove a step.
     
    Last edited: Dec 11, 2017
  3. poccix

    poccix

    Joined:
    Dec 5, 2017
    Posts:
    2
    I have adjust the code a little bit for a better view on the problem:
    Code (CSharp):
    1. Transform[] transformList = nodes.GetComponentsInChildren<Transform>();
    2.  
    3. for (int i = 0; i < transformList.Length; i++) {
    4.     if(transformList[i].gameObject != nodes.gameObject) {
    5.  
    6.         GameObject thisObj = transformList[i].gameObject;
    7.  
    8.         Debug.Log(thisObj.transform.position.x); // RETURN 7,29 - so works!
    9.  
    10.         NodeScript thisScript = thisObj.GetComponent<NodeScript>();
    11.         if(thisScript == null) {
    12.             Debug.Log("is null..."); // Does not return, so the script is not null!
    13.         }
    14.         else {
    15.             Debug.Log("Passes?"); // This returned, so i can verify the script is not null
    16.  
    17.             Transform viewpointTransform = thisScript.getViewpoint(); // At this Point i get the NullReferenceException ( i think ), so i'll add this function to post!
    18.  
    19.             Debug.Log(viewpointTransform.position.x); // this wont be printed in console
    20.         }
    21.     }
    22. }
    and the functions i mentioned:
    Code (CSharp):
    1. // On top of class
    2. public GameObject viewPoint;
    3.  
    4. // the function
    5. public Transform getViewpoint() {
    6.     Transform viewTransform = viewPoint.GetComponent<Transform>();
    7.     return viewTransform;
    8. }
    Also, thanks for fast reply !

    Best Regards,
    poccix
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I would assume the null ref is happening inside the getViewpoint() method. viewPoint isn't assigned that we can see, so unless you're assigning it in the Inspector or in another script somewhere, it would definitely be null.
     
    JoeStrout likes this.