Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by satishkumarlaksh · Apr 02, 2018 at 09:36 AM · buttons

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 02, 2018 at 09:47 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image satishkumarlaksh · Apr 02, 2018 at 09:56 AM 0
Share

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.

avatar image augivision · Apr 02, 2018 at 09:35 PM 0
Share

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.

avatar image satishkumarlaksh · Apr 03, 2018 at 05:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges