Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

returning a int throwing a NullReferenceException

Discussion in 'Scripting' started by Sean-Powell, Jun 29, 2015.

  1. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    I am calling a class with this:
    Code (CSharp):
    1. int urainium = GetComponent<ResorceCounter> ().getUranium ();
    the method it is calling is:
    Code (CSharp):
    1. public int getUranium(){
    2.         return UraniumCount;
    3.     }
    this is throwing the error:
    NullReferenceException: Object reference not set to an instance of an object
    SpawnMiner.Start () (at Assets/Scripts/Gui Scripts/SpawnMiner.cs:13)
    UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:137)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:602)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:744)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()

    I am not sure why it is doing this.
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Seems like no component of type ResorceCounter is present on the gameObject. Thus calling a method on that is not possible.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    is that line in the script "SpawnMiner.cs" and is it line 13?

    have a sneaking suspicion that you've misspelt ResourceCounter
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    In this case it's quite clear that the null reference is coming from GetComponent<ResorceCounter>(), however in many cases it can be hard to figure out exactly whats giving the error when you are nesting function calls in one line like that.

    It's generally much better to split it up into multiple lines, like so:
    Code (csharp):
    1.  
    2. ResorceCounter rc = GetComponent<ResorceCounter> ();
    3. int urainium = rc.getUranium ();
    If you do it like this, you will instantly see if it's GetComponent<ResorceCounter>() that's throwing the exception, or if it's int urainium = rc.getUranium().
     
  5. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    yea that is the line
    nah i made that mistake in the class name :).
     
    LeftyRighty likes this.
  6. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    it is the int uranium = rc.getUranium that is throwing the error, so here is the rest of the class,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class ResorceCounter : MonoBehaviour {
    6.     public Text Uranium;
    7.     public Text Titanium;
    8.     public Text CurrentUnit;
    9.     public Text MaxUnit;
    10.  
    11.     public int UraniumCount  = 250;
    12.     public int TitaniumCount = 500;
    13.     public int CurrentUnitCount = 0;
    14.     public int MaxUnitsCount = 10;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         Uranium.text = UraniumCount + "";
    19.         Titanium.text = TitaniumCount + "";
    20.         CurrentUnit.text  = CurrentUnitCount + "";
    21.         MaxUnit.text  = MaxUnitsCount + "";
    22.     }
    23.     //returns the uranium left
    24.     public int getUranium(){
    25.         return UraniumCount;
    26.     }
    27.  
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, so it's looking a lot like their isn't a ResorceCounter on the gameobject the original script you posted is on. Has to be on the same gameobject for GetComponent<>() to see it, not a child or another gameobject
     
  8. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    ok thanks
     
  9. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    i am having some issues how would you do it if it was on another gameobject as i can see no other way of doing it