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 pborg · Jun 05, 2013 at 10:04 PM · guimenudropdown

How to initialize a boolean only once when used in a function when called OnGUI

I've been building a dropdown function to place online for others trying to make something similar. I want to be able to use this same function in multiple instances throughout a GUI. I couldn't find a way to return more than one variable and this will later be return the choice of the menu. Here's what I have so far:

 var show : boolean = false;
 function List(headbox : Rect, dropheight : float, listheader : String, list : String[])
 {
     //reference # for list element
     var selection : int = 0;
     //fullbox starts at the bottom of the headbox
     var fullbox : Rect = Rect(headbox.xMin, headbox.yMax, headbox.width, dropheight);
     if(GUI.Button(headbox, listheader + ": " + list[selection]))
     {
         if(show != true)
             show = true;
         else
             show = false;
     }
     if(show)
     {
         GUI.Box(fullbox, " ");
         for(var l : int = 0; l < list.length; l++)
         {
             var entryystartpoint : float = fullbox.yMin + (l * (fullbox.height/list.length));
             var entryheight : float = (fullbox.height/list.length);
             if(GUI.Button(Rect(fullbox.xMin, entryystartpoint,fullbox.width, entryheight), list[l]))
             {
                 selection = l;
                 show = false;
             }
         }
     }
 }

Sample calling:

 var lister : String[] = new String[4];
 lister[0] = "element 1";
 lister[1] = "element 2";
 lister[2] = "element 3";
 lister[3] = "element 4";

 function OnGUI()
 {
     List(Rect(50, 150, 150, 50), 150, "Choose one", lister);
 }

I've seen several attempts online and the unify javascript example doesn't work so is there a way to send multiple variables back or pass by reference the Boolean? Thanks!

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

Answer by dandago · Jun 05, 2013 at 10:52 PM

One way (not quite popular) is to use an out parameter. You can then return the boolean via the out parameter.

Another way is to put your current return value and the boolean into a class, and return that.

It's up to you to choose which is best for your scenario.

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 Benproductions1 · Jun 05, 2013 at 10:52 PM 0
Share

out doesn't work in javascript

avatar image pborg · Jun 05, 2013 at 11:02 PM 0
Share

I know you can return a class and organize it that way. Can you give me an example of setting up a basic class and returning it. I've tried before but haven't been able to get it to work in javascript or unity for that matter. (I haven't used javascript outside of Unity) Thank you

avatar image Benproductions1 · Jun 05, 2013 at 11:37 PM 0
Share

@pborg Just look it up, there are plenty of tutorials out there for creating classes

avatar image pborg · Jun 05, 2013 at 11:46 PM 0
Share

never$$anonymous$$d I figured it out

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

16 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

Related Questions

How to use options in the Dropdown menu UI? 2 Answers

Dropdown button... 0 Answers

FadeIn/FadeOut GUI Menu and Text 1 Answer

When I pause my game and enable canvas and then resume my keyboard starts controlling the menu... 1 Answer

Paused Menu backdrop 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