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
1
Question by Ryujose · May 25, 2015 at 04:11 PM · uiobjectcanvastoggle

How can I reference a Toggle?

Hello community.

How can I reference a toggle with the new canvas UI? I've tried to reference by name and tag but it doesn't Works.

Here's a piece of my code.

  public void OpcionesKeyboard()
     {
         accelerometerJugador1.gameObject.name.Equals("accelerometerJugador1");
         keyboardJugador1.gameObject.name.Equals("keyboardJugador1");
 
         if (keyboardJugador1.onValueChanged.Equals(true))
         {}
 else if (keyboardJugador1.onValueChanged.Equals(false))
         {
             accelerometerJugador1.onValueChanged.Equals(true);
         }
 
 

or

 public void OpcionesKeyboard()
         {
             accelerometerJugador1.gameObject.tag.Equals("accelerometerJugador1UI");
             keyboardJugador1.gameObject.tag.Equals("keyboardJugador1UI");
     
             if (keyboardJugador1.onValueChanged.Equals(true))
             {}
     else if (keyboardJugador1.onValueChanged.Equals(false))
             {
                 accelerometerJugador1.onValueChanged.Equals(true);
             }
 

Thanks for the help.

Regards.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by rhynodegreat · May 25, 2015 at 04:24 PM

You need to use using UnityEngine.UI at the top of the file. Then you can just create a public Toggle myToggle field that can be assigned in the inspector.

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 Ryujose · May 25, 2015 at 05:19 PM 0
Share

I've created it with "private Toggle keyboardJugador1" but I don't know where to assign it on the inspector.

The place "On value Changed (Boolean)" is for the function. But there's a place for reference it?

I mean I've tried to assign by tag and name but it's useless. There's another way? If there are, can you show me where it is?

Thanks rhynodegreat.

avatar image rhynodegreat · May 26, 2015 at 12:09 AM 1
Share

If you make it public or use the [System.SerializeField] attribute, then you can assign it in the inspector. Also, in the inspector for the toggle, you can set it up so that it calls the function you want.

avatar image Ryujose · May 26, 2015 at 12:24 AM 0
Share

Good to know it. Thanks, I'll try and see how it Works. Edit: By the way, what reference use "[System.SerializeField]" attribute?

avatar image
1

Answer by Phenoxon · May 25, 2015 at 06:00 PM

You will probably want to use GameObject.Find() to locate your Toggle by name:

 var myToggle : GameObject;
 // This will return the game object named Hand in the scene.
 hand = GameObject.Find("myToggle");

Other similar functions (such as FindGameObjectsWithTag, FindObjectOfType, FindObjectsOfType, etc.) are listed in the manual entry for GameObject.

To find all active Toggles by tag:

 Toggle[] myToggles = GameObject.FindGameObjectsWithTag ("myToggleTag");

To find all Toggles in the scene (irrespective of tag), you can use:

 Toggle[] myToggles = FindObjectsOfType(typeof(Toggle)) as Toggle[];

Hope this helps.

  • P.S. To expose a variable in the Inspector, you will need to make it Public.

  • P.S.2 I'm new to Unity as well. Treat my answers with some suspicion as they might not be correct/efficient.

Comment
Add comment · Show 1 · 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 Ryujose · May 25, 2015 at 07:32 PM 0
Share

That's a good point to know. But I'm declaring the class Toggle like Private Toggle "name" to call its methods. Then if I declare it like GameObject I can't do nothing with Toggle class.

The problem is that by code I can't search that Toggle on my hierarchy. I'll like to have inspector more clean and don't do assigns of grab and drop.

By the moment I've assigned with Public declaration on inspector with drag and drop on my script like you told me.

Thanks for the help.

If anyone know how to assign by code it will be awesome.

avatar image
0

Answer by BlueAero · Jun 13, 2016 at 04:11 AM

GameObject toggle; Toggle soundToggle;

void Start() { toggle = GameObject.FindGameObjectWithTag("Sound"); soundToggle = toggle.GetComponent();
SetToggles(); }

void SetToggles() { if (soundOn) { soundToggle.isOn = true; } else { soundToggle.isOn = false; } } // end SetToggles()

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

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

22 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

Related Questions

How Can I save my values with Toggles in UI canvas 4.6 through scenes? 3 Answers

UI canvas, toggle question 0 Answers

UI button does not press if colliding with other UI object 1 Answer

Canvas dropdown is buggy and i don't know how to solve it ;-; 0 Answers

Using raycast to enable canvas that is linked to a prefabs gameobject 0 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