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 darkal · Sep 16, 2013 at 07:28 PM · javascriptgui button

make gui appear on gui button click

this is the code i just want it to show the other buttons when i click click the inventory button. i am not having any errors so thats good just not showing it when i click it

 var inbw = 0;
 var inbh = 0;
 
 var weaponhight = 100;
 var armorhight = 125;
 var clothinghight = 150;
 var jewerlyhight = 175;
 var bw = 500;
 var size = 100;
 var size1 = 25;
 
 function OnGUI()
     {
         if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory"))
         {
             if (GUI.Button(Rect(bw, weaponhight, size, size1), "Weapons"))
             {
                 
             }
             if (GUI.Button(Rect(bw, armorhight, size, size1), "Armor"))
             {
                 
             }
             if (GUI.Button(Rect(bw, clothinghight, size, size1), "Clothing"))
             {
                 
             }
             if (GUI.Button(Rect(bw, jewerlyhight, size, size1), "Jewerly"))
             {
                 
             }
         }
     }
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
1
Best Answer

Answer by oliver-jones · Sep 16, 2013 at 07:44 PM

This is actually quite a common mistake for beginners.

Currently, your inventory ("Weapons, Armour, Clothing ...") is within your:

 if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory")){

Which means that if you are to press it, the Inventory list will show ... but only for 1 frame (I believe). You need to create a boolean to trigger your GUI Inventory list:

 var inventoryActive : boolean = false;
 
 function OnGUI(){
 
     if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory")){
        //Give the opposite value (true will = false, false will = true)
        inventoryActive = !inventoryActive;
     }
     
     if(inventoryActive){
         //Put your buttons in here
     }
 }

Try that :)

Comment
Add comment · Show 4 · 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 perchik · Sep 16, 2013 at 07:47 PM 0
Share

beat me by that much!

avatar image darkal · Sep 17, 2013 at 12:20 AM 0
Share

ill try them both 2marrow at school im making a small game to get into collage ill let u both know if they work

avatar image darkal · Sep 17, 2013 at 01:20 PM 0
Share

urs does what i want perfectly ty

avatar image oliver-jones · Sep 19, 2013 at 10:47 AM 0
Share

Your welcome, don't forget to tick the answer correct that helped you. Thanks :)

avatar image
0

Answer by perchik · Sep 16, 2013 at 07:47 PM

The problem is that the if statement only executes when the button is pressed. I imagine if you click and hold the 'Inventory' button, your GUI might show up.

A way around this is to set some kind of flag or marker to tell it that you pressed the inventory button:

 var showInv: bool = false;
 
 function OnGUI()
     {
        if (GUI.Button(Rect(inbw, inbh, size, size1), "Inventory"))
        {
          showInv=true;
       }
 
       if(showInv){
          if (GUI.Button(Rect(bw, weaponhight, size, size1), "Weapons"))
          {
  
          }
          if (GUI.Button(Rect(bw, armorhight, size, size1), "Armor"))
          {
  
          }
          if (GUI.Button(Rect(bw, clothinghight, size, size1), "Clothing"))
          {
  
          }
          if (GUI.Button(Rect(bw, jewerlyhight, size, size1), "Jewerly"))
          {
  
          }
        }
   }
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 darkal · Sep 17, 2013 at 01:21 PM 0
Share

yours is good but it just didnt hide the gui after i clicked button again im sure i could have edited it easy but im 2 lazy lmao ty anyway its still a awesome script

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

17 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

Related Questions

GUI GameObject Button? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Circle GUI Button, Javascript? 1 Answer

How to activate a button? 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