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 Robomaster · Sep 05, 2012 at 05:14 AM · guibutton

GUI Button Question

Hello, the question i have is can you turn off certain parts of a script, for example i have 2 GUI Buttons but while ones on i dont want the other one one, but i dont want to make 2 separate scripts is there anyway to make it so that while ones on the the other cant be on, even thought there in the same script?

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
1

Answer by OperationDogBird · Sep 05, 2012 at 05:40 AM

Sure thats not a problem. There are a couple of approaches to this. Depending on the depth of the script you will favor one over the other. Firstly you could use a boolean method

 var showSetOne:boolean;
 
 function OnGUI()
 {
     if(showSetOne)
     {
         if(GUI.button(.....))//Do something
         if(GUI.button(.....))//Do something
     }
     else
     {
         if(GUI.button(.....))//Do something
         if(GUI.button(.....))//Do something
     }
 }
 

Now if your menu/gui setup is much more complex than just have 2 dimensions, you should use a switch case approach

 var menu:int;
 
 function OnGUI()
 {
     switch(menu)
     {
         case 0:
             //ShowGUIForGroupZero
             if(GUI.Button(.....))menu=1;
             if(GUI.Button(.....))menu=2;
             if(GUI.Button(.....))menu=3;
         break;
         case 1:
             //ShowGUIForGroupOne
             if(GUI.Button(.....))menu=2;
         break;
         case 2:
             //ShowGUIForGroupTwo
             if(GUI.Button(.....))menu=3;
         break;
     }
     //Anything outside of the switch-case with be shown regardless of menu
 }
Comment
Add comment · Show 9 · 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 Robomaster · Sep 05, 2012 at 05:51 AM 0
Share

ohhh okay i see so this would work? btw i program in C#

public bool showSetOne;

public bool showSetTwo

function OnGUI()

{ if(showSetOne)

 {

     if(GUI.button(.....))//Do something

     if(GUI.button(.....))//Do something
 }

 else

 {

     if(GUI.button(.....))//Do something

     if(GUI.button(.....))//Do something

 }

if (showSetTwo)

{

if(GUI.button(.....))//Do something

     if(GUI.button(.....))//Do something

 }

 else

 {

     if(GUI.button(.....))//Do something

     if(GUI.button(.....))//Do something
 }

}

avatar image OperationDogBird · Sep 05, 2012 at 06:05 AM 0
Share

For more than one set it would be better to use the switch case version since you now have 4 different scenarios( if(1)else if(2)else) ). The switch case is very easy to expand upon and will cause less clutter in the script since you will not need to make sure different booleans are at the proper value. Heres the C# version of switch, practically the same.

 public int menu = 0;
 
 void OnGUI()
 {
     switch(menu)
     {
         case 0:
             //Do Some $$anonymous$$enu Zero Stuff
         break;
     }
 } 
avatar image Robomaster · Sep 05, 2012 at 06:15 AM 0
Share

Wait im alittle confused sry. could you explain how this code will disable one GUI button while the other is active

avatar image OperationDogBird · Sep 05, 2012 at 06:22 AM 0
Share

the gui will ONLY run in one case at a time. Thus: if menu = 0, the gui inside of case 0 in the switch will run and none of the other cases will meet the switches condition, its the same as saying

 if(menu==0)
 {
     //Run this code only if menu is 0
 }
 if(menu==1)
 {
     //Run this code only if menu is 1
 }
 etc..
 etc..


Very very handy when you have more than 2 conditions. Since we have the variable menu at hand, we dont need to change boolean values to true and false because the switch does all that for us. Plus later down the line if you need to add a new 'set' / menu of gui items, you can just make a new case for the switch : "if menu = x" / case x: //new menu

avatar image Robomaster · Sep 05, 2012 at 06:36 AM 0
Share

ohhh okay and when you click the next button it switches the menu?

Show more comments
avatar image
0

Answer by Anusha · Sep 05, 2012 at 06:56 AM

i am guessing what you want to do is differentiate between your home page GUI [play btn,options btn, settings btn]and your in scene GUI[score,time]... so that when one shows other dosent and you want to do this in one script.

here's what you do:

 private bool menu;
 
 void Start()
 {
 menu = true;
 }
 
 void OnGUI()
 {
 if(menu)
 {
 //your homepage GUI goes here
 if(GUI.Button(new Rect(10,10,10,10),"PLAY"))
 {
 menu = false;
 }
 }
 else
 {
 //your scene GUI goes here
 //make menu = true when u want to display homepage GUI again.
 }
 }
Comment
Add comment · Show 6 · 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 Robomaster · Sep 05, 2012 at 07:44 AM 0
Share

That didnt really work for me since i needed 2 buttons so i took some of what you wrote and added some stuff but it doesent seem to be working right. could you see if i did anything wrong

using UnityEngine;

using System.Collections;

public class BUTTONS2 : $$anonymous$$onoBehaviour {

private bool menu;

// Use this for initialization

void Start () {

menu = false;

}

// Update is called once per frame

void Update () {

if(menu = false){

GetComponent().enabled = true; } if(menu = true){

GetComponent().enabled = false; } }

void OnGUI(){

if(menu){

if(GUI.Button(new Rect(100,10,100,100),"PLAY2")) { menu = false; } } &else { menu = true; } } }

avatar image OperationDogBird · Sep 05, 2012 at 07:47 AM 0
Share

$$anonymous$$enu would be a integer not a boolean. take a look at my above stated comments in both my answer and this answer. Please answer in my answer since my answer is the one you are following.

avatar image Robomaster · Sep 05, 2012 at 07:53 AM 0
Share

okay sry and in my last example i did in your answer did i do it right

avatar image Anusha · Sep 05, 2012 at 08:38 AM 0
Share

@OperationDogBird: Sir i did not say my answer was best or optimal... i just took an example and a very simple one , so that Robomaster could get an idea about how to go about things. it was just a simplest solution... about the options menu and all... IF they are present then i am sure by the time a devoloper reaches that far in coding he will be able to implement the best code.... i was just helping in getting him a headstart....in a simple way...

avatar image Anusha · Sep 05, 2012 at 08:43 AM 0
Share

@Robomaster: when you use a condition you are to use '==' not '=' so if(menu == false){}... not if(menu = false){} also if its a boolean just if(menu){ }; is enough....

thats the mistake in the above code you pasted.

Show more comments
avatar image
0

Answer by Anusha · Sep 05, 2012 at 08:51 AM

     using UnityEngine;
     
     using System.Collections;
     
     public class BUTTONS2 : MonoBehaviour {
     
     private bool menu;
     
     // Use this for initialization
     
     void Start () {
     
     menu = false;
     
     }
     
     // Update is called once per frame
     
     void Update () 
     { 
     
     }
     
     void OnGUI()
     {
     
     if(menu)
     {    
     if(GUI.Button(new Rect(100,10,100,100),"PLAY2")) 
     { 
     menu = false;
     GetComponent().enabled = true; 
      } 
     } 
     else 
     { 
    if(GUI.Button(new Rect(100,10,100,100),"PLAY1")) 
     {
        menu = true; 
        GetComponent().enabled = false; 
    }
     } 
     } 
     }

your code in a better way... no need update...your just redoing what can be done in OnGUI()....

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

9 People are following this question.

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

Related Questions

How to add intermediate tooltip to GUI button? 1 Answer

GUI Button Jagged Edges JavaScript 0 Answers

Automatic Wrap 2 Answers

GUI Button sound problem, don´t work 1 Answer

GUI.button Texture shows up in Unity but doesn't show on android device. 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