Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to change a button in Unity 5?

Discussion in 'UGUI & TextMesh Pro' started by ejectedmatrix_dev, Mar 14, 2015.

  1. ejectedmatrix_dev

    ejectedmatrix_dev

    Joined:
    Mar 14, 2015
    Posts:
    3
    Hello, I have a button script to change a button but it won't work. I have debugged and tried everything but none will work. Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. using UnityEngine.Events;
    6. using UnityEngine.Sprites;
    7.  
    8. public class ChangeScene : MonoBehaviour {
    9.     Text txt;
    10.     // Use this for initialization
    11.     void Start () {
    12.         txt = gameObject.GetComponent<Text>();
    13.     }
    14.     IEnumerator wait (int time) {
    15.         yield return new WaitForSeconds(time);
    16.         Debug.Log("wait");
    17.     }
    18.     public void changeText(int pass){
    19.         Debug.Log("start");
    20.         if(pass == 1){
    21.             txt.text = "Test in progress...";
    22.             Debug.Log("work1");
    23.             wait(5);
    24.             Debug.Log("work2");
    25.             txt.text = "";
    26.         }
    27.         else {
    28.             txt.text = "Error, please report to Sarmad Wahab. Code: KB56";
    29.             Debug.Log("Work");
    30.             wait(4);
    31.             txt.text = "";
    32.         }
    33.     }
    It gives me the "start" but not "work" or "wait";
    and when I click the button it gives me that ^ and this :

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. ChangeScene.changeText (Int32 pass) (at Assets/ChangeScene.cs:21)
    3. UnityEngine.Events.InvokableCall`1[System.Int32].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:141)
    4. UnityEngine.Events.CachedInvokableCall`1[System.Int32].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:259)
    5. UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)
    6. UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)
    7. UnityEngine.Events.UnityEvent
    Thanks for the help;
    and I may reply with a semicolon at the end of a sentence; (thats just my addiction to semicolons, lol)
     
  2. nesseggman

    nesseggman

    Joined:
    Dec 20, 2012
    Posts:
    22
    Well, it seems to be getting stuck on line 21, saying the Object reference is not set to an instance of an object. So it seems like your txt variable is not actually getting a Text component in Start, I assume.

    Do you have this script attached to something that actually has a Text component? If it's in a child, you'll have to use GetComponentInChildren<> and make sure there's only 1 text for it to find (or that it's the next in the heirarchy).

    Also, you don't need to do gameObject.GetComponent. You can just use GetComponent<Text>.
     
    ejectedmatrix_dev likes this.
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    For a Button you will want to do GetComponentInChildren() as the text GO is a child of the button control.

    I believe @nesseggman is right that your txt reference is likely null.

    If doing references like this, its always better to put some error trapping in to avoid issues like this, such as:
    Code (CSharp):
    1. txt = gameObject.GetComponent<Text>();
    2. if(txt == null)
    3.     Debug.LogError("txt reference not found");
     
    ejectedmatrix_dev likes this.
  4. ejectedmatrix_dev

    ejectedmatrix_dev

    Joined:
    Mar 14, 2015
    Posts:
    3
    I have it attached to a gameobject (empty) called _Manager, when the button is clicked, it tells _MAnager to launch the script with pass to be assigend to 1.
     
  5. nesseggman

    nesseggman

    Joined:
    Dec 20, 2012
    Posts:
    22
    Then what is happening is that at Start, the script is searching for a Text object that is a component of _Manager itself. If it doesn't find one, GetComponent returns null, so txt == null. In line 21, you're trying to access the text of txt, but txt is null, which throws the error and stops the code from continuing into the "work1" debug line.

    You probably want to attach this script to your button, or in some other way make sure that txt is being set to the Text component that you want. If all of these are preloaded things in the scene (_Manager, the Button, and its Text), you can make your variable txt public and just assign it in the inspector.
     
    ejectedmatrix_dev likes this.