Search Unity

UI Toggle Issue: NullReferenceException

Discussion in 'Scripting' started by skantron, Feb 19, 2019.

  1. skantron

    skantron

    Joined:
    May 5, 2011
    Posts:
    35
    I have a script that changes child text on a toggle ui object. It works, but it keeps on throwing an error
    "
    NullReferenceException: Object reference not set to an instance of an object
    ToggleText.Update () (at Assets/Scripts/ToggleText.cs:18)
    "

    I would like to fix the error, and then also see if I can get advice on how change it so it doesn't check every frame, but instead only switches the text when the bool state is changed.

    Script is
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ToggleText : MonoBehaviour
    5. {
    6.     Toggle ui_Toggle;
    7.     public Text ui_Text;
    8.     public string A;
    9.     public string B;
    10.  
    11.     void Start()
    12.     {
    13.         ui_Toggle = GetComponent<Toggle>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (ui_Toggle.isOn)
    19.         {
    20.             ui_Text.text = A;
    21.         }
    22.  
    23.         else
    24.         {
    25.             ui_Text.text = B;
    26.         }
    27.     }
    28. }
    29.  
    Thanks
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    The only way it throws a null error on line 18 is if ui_Toggle is null.

    As far as not doing this in Update, if you take a look at the Toggle component, it has an On Value Changed event. You can hook in this event so when the value changes, it calls a method.
     
  3. skantron

    skantron

    Joined:
    May 5, 2011
    Posts:
    35
    So, if ui_Toggle is null, why is it null? and how do I make it not null. My assumption was that ui_Toggle was the toggle ui component, and it was getting the value from that successfully (In that the script was working, and the text is toggling as I was expecting). If I make ui_Toggle public and simply drag the toggle object into it, I still get the same error. I am very inexperienced with this, so I am probably missing something basic, but I seem to be missing it for sure.
    thanks
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Are you sure you don't have a duplicate of the script on another object in the scene that doesn't have a toggle component on it?
     
    skantron likes this.
  5. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Try adding `[RequireComponent(typeof(Toggle))]` just before `public class ToggleText`, and see what happens in your editor.
     
    skantron likes this.
  6. skantron

    skantron

    Joined:
    May 5, 2011
    Posts:
    35
    There was in fact a duplicate of the script hiding on another object in the scene. Usually it is me doing something dumb. Thanks!

    L:F
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    No problem, glad you found it. I would still suggest you look into the On Value Changed Event on the toggle instead of using Update as well.