Search Unity

Feedback Yet another NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by khrysller, May 7, 2020.

  1. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Hi.

    I am new to scripting and I am having trouble trying to access a function from another script.

    Relevant part of the GameController.cs:
    Code (CSharp):
    1.     public Slider slider;
    2.     public float FillSpeed = 0.5f;
    3.     public float targetProgress = 0;
    4.  
    5.     public void Update () {
    6.  
    7.         Progressbarupdate();
    8.     }
    9.  
    10.     public void IncrementProgress(float newProgress)
    11.     {
    12.         Debug.Log("I am here");
    13.         targetProgress = slider.value + newProgress;
    14.     }
    15.  
    16.     public void Progressbarupdate()
    17.     {
    18.         if (slider.value < targetProgress)
    19.             slider.value += FillSpeed * Time.deltaTime;
    20.     }
    Relevant part of the CollectionCoin.cs
    Code (CSharp):
    1. public class CollectCoins : MonoBehaviour
    2. {
    3.     GameController GC = new GameController();
    4.  
    5.     public void OnTriggerEnter(Collider other)
    6.     {
    7.         if (gameObject.CompareTag("Coin"))
    8.         {
    9.             GameController.scoreValue += 50;
    10.             GC.IncrementProgress(0.75f);
    11.             Destroy(gameObject);
    12.         }
    13.     }
    14. }

    When I trigger the function in the console I see the "I am here" as a log, so I can see trigger goes to the function but adicionally I receive an error

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. GameController.IncrementProgress (System.Single newProgress) (at Assets/Scripts/Atual/GameController.cs:241)
    3. CollectCoins.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Atual/CollectCoins.cs:14)
    line 241 in GameController is targetProgress = slider.value + newProgress; and line 14 in CollectCoins.cs is GC.IncrementProgress(0.75f);

    And I have the Slider attached

    GC.PNG

    Thanks for any help on this.
     
    Last edited: May 7, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    You haven't assigned a Slider to your slider field. You can do so by dragging one from your scene hierarchy onto your GameController component in the slider field.
     
  3. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    I had it before, just didnt mention it on the post... I just updatet it for future visitors. Thanks...
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Try changing your log statement to
    Code (CSharp):
    1. Debug.Log($"Slider is {slider}");
    What does that print out?
     
  5. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Just Slider is

    Code (CSharp):
    1. Slider is
    2. UnityEngine.Debug:Log(Object)
    3. GameController:IncrementProgress(Single) (at Assets/Scripts/Atual/GameController.cs:241)
    4. CollectCoins:OnTriggerEnter(Collider) (at Assets/Scripts/Atual/CollectCoins.cs:14)
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Something is setting your slider to null. Otherwise it would print the name of the GameObject that the Slider is attached to.

    Is the slider in your scene, or is it an asset? Also are you certain there is only ONE GameController object in your scene?
     
  7. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    There is only one reference of this script in the scene. The slider is in the scene, active. Well, I did another test based on your suggestion. I called the function on the same CS file that has the function... and it worked

    Code (CSharp):
    1. Slider is Slider (UnityEngine.UI.Slider)
    2. UnityEngine.Debug:Log(Object)
    3. GameController:IncrementProgress(Single) (at Assets/Scripts/Atual/GameController.cs:244)
    4. GameController:Update() (at Assets/Scripts/Atual/GameController.cs:93)
    But if I call it from another script the error still coming
     
    Last edited: May 7, 2020
  8. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125