- Home /
Text component of Button subclass returns null
Hi All,
I try to subclass Button in unity so that I can have custom member variables to it such as Url. I code on the lines of:
public class ButtonTest : Button, IPointerClickHandler {
// Getters and setters
void Start(){
baseButton = GetComponent<Button>();
if(onClick != null)
baseButton.onClick = onClick;
Debug.Log("Basebutton = " + baseButton + ", text = " + baseButton.GetComponentInChildren<Text>() );
//update label
baseButton.GetComponentInChildren<Text>().text = text;
}
But the line "baseButton.GetComponentInChildren<Text>()"
returns null.
Should I explicitly add a Text component to the subclass? Doesn't it inherit the component from the base class?.
I found a related question at: https://stackoverflow.com/questions/41194515/unity-create-ui-control-from-script which suggests that Unity doesn't provide default backgrounds, images when creating UI controls.
Why are you getting the Button since that class is the Button?
you could try:
this.gameObject.GetComponentInChildren<Text>(true).text = text;
that would ensure to find the component if the object is null.
But it seems you are expecting things that are not granted.
"Should I explicitly add a Text component to the subclass? Doesn't it inherit the component from the base class?."
There is no Text reference in Button class, so you won't inherit one.
Answer by augivision · Apr 02, 2018 at 09:43 AM
You could just add
using UnityEngine.UI;
at the top
Then add a public text field
public Text text1;
text1.text = text; //instead of GetComponentInChildren<Text>().text = text;
Then select the object in the hierarchy that this script is attached to and drag the text you want to change into the text1 field.
This is a much easier way to do things in my opinion. Simply drag & drop.
Thanks. I get what you say. However, I wish to create buttons dynamically and set custom fields like Url to it. On button click, I would use the URL to fetch data from a web server. I would host a few buttons of this type in a grid view. So each button in the grid view will have its own URL to act upon.
So for something like that, I would attach a class to each button that has a
public string Link;
Then add a public method for OpenLink()
public void OpenLink()
{
Application.OpenURL(Link);
}
Then either add an OnClick Event or access this class from your touch script
public class TouchHandler : $$anonymous$$onoBehaviour
{
private GothicEventHandler gothicHandler;
private YouTubePlayer youTubePlayer;
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity))
{
if (hit.transform.gameObject.name == ("HarmonyVideo"))
{
Application.OpenURL("https://www.harmonyextracts.com/");
}
if (hit.collider.tag == ("Ticket"))
{
gothicHandler = hit.collider.GetComponentInParent<GothicEventHandler>();
gothicHandler.OpenLink();
}
if (hit.collider.tag == ("GothicEvent"))
{
gothicHandler = hit.collider.GetComponent<GothicEventHandler>();
gothicHandler.ToggleActive();
}
if (hit.collider.tag == ("YouTubePlayer"))
{
youTubePlayer = hit.collider.GetComponent<YouTubePlayer>();
youTubePlayer.OpenYouTubeLink();
}
}
}
}
}
}
I made an app where each Theatre event has a different website URL on it and when you touch it the website opens. You can always change the string via code.
Does this make sense? It seems like what you want to do. You can also make a bunch of strings and create a new method for each button to add to the click event.
Thanks for the suggestion and posting the code. I went ahead with a workaround for now but this is something that I will consider for the future, as the button component might require to hold additional attributes.
Your answer
Follow this Question
Related Questions
Moving buttons 1 Answer
Problems while setting up buttons on the GUI 0 Answers
Main Menu GUI Button - Animation? 1 Answer
GUI: Overlapping and Box size collision 1 Answer
Main menu help needed (C#) 2 Answers