- Home /
Question by
Chocolade · Apr 11, 2017 at 08:26 AM ·
c#scripting problemscript.
How can i change a button text from "Button" to example "Button1" ?
In the Hierarchy i have a Canvas under it a Button and under the Button a Text. In the script i did:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NodesGenerator : MonoBehaviour {
public Button btn;
private void Start()
{
btn.GetComponent<Text>().text = "Button1";
}
But getting null exception on this line:
btn.GetComponent<Text>().text = "Button1";
I dragged the button to the script in the inspector it's not the button that is null.
Comment
Best Answer
Answer by UnityCoach · Apr 11, 2017 at 08:29 AM
The Text is a Child of the Button object. Try
btn.GetComponentInChildren<Text>().text = "Button1";