Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dr3th · Mar 16, 2015 at 01:38 PM · c#unity5getcomponenttoggletoggle button

Access UI.Toggle from c# CodeBehind -How does one set the Toggle created in the front end of Unity?

I would like to set the IsOn Property to True. The API states that one can only get the "IsOn" values of when it was instatiated. There is no setter for the property it seems.

 GameObject togGameObj= GameObject.Find("MyToggle");
 Toggle myTog = togGameObj.GetComponent<Toggle>();
 myTog.IsOn = true;
 
   
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by B3aT · Mar 16, 2015 at 02:50 PM

I do not think you can control the UI elements directly, you must use the EventSystem class. So trigger it old fashion web way, simulate a click.

 ExecuteEvents.Execute<IPointerClickHandler>(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);

http://docs.unity3d.com/Manual/SupportedEvents.html

http://docs.unity3d.com/Manual/MessagingSystem.html

Comment
Add comment · Show 5 · 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 dr3th · Mar 16, 2015 at 02:59 PM 0
Share

Hi B3aT, I can change the value of a "Text" UI control directy. The following example works fine. So I assume there could still be a similar functionality with "Toggle" UI control:

 GameObject textGameObj= GameObject.Find("myText");
 Text myText = textGameObj.GetComponent<Text>();
 $$anonymous$$yText.Text = "Hello World"

avatar image B3aT · Mar 16, 2015 at 03:12 PM 0
Share

Yes but that is a property. The toggle is a state, and in all articles/videos/tutorial you can see is all about the new eventSystem. Theres even an example where a character and interact with the world space UI canvas by touching it, using events.

avatar image dr3th · Mar 17, 2015 at 02:00 PM 0
Share

HI B3at,

What Articles/Videos/Tutorials please, can you reference what your quoating here?

How I understand it from a Program$$anonymous$$g perspective, Toggle is a class, the IsOn is a property with getter and a setter. $$anonymous$$y issue is that the Setter for the IsOn property doesn't seem to do anything with the Property.

I guess my question now becomes is there another property or even function that allows one to set the IsOn Property from the codebehind.

avatar image B3aT · Mar 17, 2015 at 02:06 PM 0
Share

Hi, One is when they first presented the UI (at a conference, do not have the time to search for it), and the tutorials sections presents how you work with the UI, all events go trough EventSystem (OnClick attach callbacks).

As your answer, no there is no way to do this. http://docs.unity3d.com/ScriptReference/UI.Toggle.html Like there is no way to click on a button http://docs.unity3d.com/ScriptReference/UI.Button.html

avatar image dr3th · Mar 17, 2015 at 02:53 PM 0
Share

Cheers, I shall have work around it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Persistent visual states for Toggle-as-a-button 0 Answers

Why we have to use getcomponent instead of a simple reference ? 2 Answers

Toggles/Toggle Groups Question 0 Answers

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

What is the diffrence between Reference and GetComponent ? 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