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 devsolo · Oct 02, 2015 at 02:12 AM · script.toggle

How to change Toggle Text using C#?

Hi Guys,

Can someone please explain to me how I can change the display Text/Label of a Toggle from within C# code?

I'm new to Unity but have many years of C# experience.

Thanks in advance!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Suddoha · Oct 02, 2015 at 02:19 AM

You could have a public variable of type Text (which belongs to the UnityEngine.UI namespace) in your script. The object that you attach this script to will then show a slot in the inspector. You can drag&drop any gameobject onto that slot that has a Text component or use code to find the appropriate text component if you don't like to assign it there.

In the script, you can then simply use something like

 toggleText.text = "some text";

You could also have the variable of type Toggle, GameObject, Transform but then you'd have to find the text component in the components or children first, depending on the hierarchy.

Comment
Add comment · 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
0

Answer by devsolo · Oct 02, 2015 at 07:20 AM

Hi Suddoha,

I don't really want to do anything at design time, I would prefer finding the Toggle and then changing the Text at runtime using code, as a future requirement will be to create N number of Toggles based on an external data source and access these dynamically within code.

I already have the code defined, which finds the Toggle and can access the child components. But there doesn't seem to be a Text component or property to work against.

Currently from what I can see, the hierarchy within the Toggle is: Toggle - Background - Label

These are the only child components I can see, the Toggle has 2 children, Background and Label. Label is actually just a RectTransform. And Label doesn't have any children. So I'm not sure how to change the text?

Comment
Add comment · Show 2 · 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 saschandroid · Oct 02, 2015 at 07:38 AM 0
Share

The label has a text component (default).

avatar image Suddoha · Oct 02, 2015 at 11:16 AM 0
Share

Well, then just use GetComponent<Text> on the Label's gameobject, or if you're sure that there's only one text component, you can also use GetComponentInChildren<Text>(). They both have a plural version too GetComponents<...> and GetComponentsInChildren<...> respectively, but then you'll (of course) get an array of these components.

You can do that with every component that is attached to gameObjects. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I Turn Off Toggle in a ToggleGroup ? 1 Answer

how do i toggle image effects on my camera with a script 1 Answer

Toggle script with button 0 Answers

Accessing Variables from another object Script 1 Answer

Flip over car after tips over? 1 Answer


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