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 ExTheSea · May 26, 2012 at 05:19 PM · guitoggleswitchcheck

Toggle Switch

Hey i have a little problem and i hope someone can help me.

I want to make a list of GUI.Toggle and when you have a toggle on and then click on another one the active one should turn off.

I tried something and it kind of worked:

 toggletest=GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1");
 toggletest2=GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2");
 if(toggletest)
     toggletest2=false;
 if(toggletest2)
     toggletest=false;

It only works when you have the second one selected and then click on the first one.

If you know how to fix the code above or if you know another, better way to do it I would be happy to read it. Help is much appreciated.

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 Mukilan_1600 · Apr 30, 2016 at 11:01 PM 0
Share

I Have An Toggle Created in $$anonymous$$y Editor. So Iam Not Using the OnGUI() Function And This Function Seems Impossible Cause When I Press One Toggle I want To $$anonymous$$ake The Other Toggle Off. And I Dont Have One Or Two Toggles So Its Pretty Tough Please Help $$anonymous$$e. Heres The Code:

 public GameObject[] comtb;
 
 
 void Update()
 {
 comtb = GameObject.FindGameObjectsWithTag ("components");
 
 
         foreach(GameObject compnnts in comtb)
         {
             if (compnnts.GetComponent<Toggle> ()) {
 
                 compnnts.GetComponent<Toggle> ().isOn = SetOneCheck;
             }
             
             }
 
     }
 
 
     bool SetOneCheck()
     {
         foreach(GameObject compnnts in comtb)
         {
             compnnts.GetComponent<Toggle> ().isOn = false;
         }
 
         return true;
     }
 
 





5 Replies

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

Answer by ExTheSea · May 26, 2012 at 05:28 PM

I found it out myself:

   //in OnGUI(){
 if(GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1")) toggletest=setMeOnly();
 if(GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2")) toggletest2=setMeOnly();
 
 ...}
 
 function setMeOnly():boolean{
 
   toggletest = toggletest2 = false;
   return true;
 }

Still if someone knows an even better way then just post it as an answer.

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 MrPrajapat · Mar 25, 2014 at 02:04 PM 0
Share

Ya working!! Excellent

avatar image Neo-Gamefactory · Dec 03, 2014 at 03:53 PM 0
Share

This is a Brilliant Solution :)

avatar image
1

Answer by Xaurrien · May 31, 2012 at 03:11 PM

My way to do it :

 if(GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1")) {
    toggletest = true;
    toggletest2 = false;
 }
 
 if(GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2")) {
    toggletest = false;
    toggletest2 = true;
 }
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 ExTheSea · May 31, 2012 at 04:15 PM 0
Share

This is nearly the same thing that i'm using only it will take up more space when there will be more GUI.Toggle's.

The question was closed 4 days ago but still thanks for your answer as you are new to unity answers.

avatar image
0

Answer by JeanClaude2010 · May 31, 2012 at 05:04 PM

HI everybody,

I'm facing the same problem. The thing is I have tried each of your solutions, and searched for it, but I can't manage to have it working in C# oO

Any help greatly appreciated !

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 ExTheSea · May 31, 2012 at 05:58 PM 0
Share

What is your problem?? Which error do you get? I'm not used to work with C# in unity but from what i know it should be quite similar except of function being void in c. Is it necessary that it is in C?

avatar image ExTheSea · May 31, 2012 at 06:06 PM 0
Share

Look at my new answer. I converted my code in C but i'm not 100 percent sure it willl work. Give it a try.

avatar image
0

Answer by ExTheSea · May 31, 2012 at 06:05 PM

This is how i think my code would look like in C. I did this for jeanclaude2010

Hope this works because like i said i'm not used to work with C

 public bool toogletest1 = false;
 public bool toogletest2 = false;
 
 void OnGUI(){
 
 if(GUI.Toggle(new Rect(200,200,50,50),toggletest,"tes1")) toggletest=setMeOnly();
 if(GUI.Toggle(new Rect(270,200,50,50),toggletest2,"tes2")) toggletest2=setMeOnly();
 
 }
 
 void setMeOnly():bool{   //not entirely sure if bool works maybe its boolean 
 
   toggletest = toggletest2 = false;
   return true;
 }
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 Neo-Gamefactory · Dec 03, 2014 at 03:55 PM 0
Share

bool set$$anonymous$$eOnly() { }

is correct

avatar image
0

Answer by JeanClaude2010 · May 31, 2012 at 07:22 PM

Works perfectly, I had forgotten to use public variables. Many thanks !

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

8 People are following this question.

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

Related Questions

Switching between set of components 1 Answer

Whats wrong with my GUI.Toggle? 2 Answers

Is there a multiple selection ugui toggle group? 1 Answer

how to make a Horizontal slider & toggle in C# with GUI textures 1 Answer

GUI toggle on click, unless on GUI. 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