Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rafal Sankowski · Apr 12, 2010 at 10:48 PM · guibuttonchangevalues

GUI Buttons to change values.

Hi. So I want to make clickalbe buttons that changes variables. I have following code:

var walkSpeed = PlatformerControllerMovement.walkSpeed; var runSpeed = PlatformerControllerMovement.runSpeed; var inAirControlAcceleration = PlatformerControllerMovement.runSpeed; var gravity = PlatformerControllerMovement.gravity; var maxFallSpeed = PlatformerControllerMovement.maxFallSpeed; var speedSmooth = PlatformerControllerMovement.speedSmoothing; var rotateSmooth = PlatformerControllerMovement.rotationSmoothing;

var height = PlatformerControllerJumping.height; var extraHeight = PlatformerControllerJumping.extraHeight; var doubleJumpHeight = PlatformerControllerJumping.doubleJumpHeight;

function OnGUI () { chooseButton(0,0,"walkSpeed", walkSpeed); chooseButton(1,0,"runSpeed", runSpeed); chooseButton(2,0,"inAirCtrlAcc", inAirControlAcceleration); chooseButton(3,0,"gravity", gravity); chooseButton(4,0,"maxFallSpeed", maxFallSpeed); chooseButton(5,0,"speedSmooth", speedSmooth); chooseButton(6,0,"rotateSmooth", rotateSmooth);

 chooseButton(0,1,"hate", height);
 chooseButton(1,1,"extraHate", extraHeight);
 chooseButton(2,1,"doubleHate", doubleJumpHeight);

}

function chooseButton (posX, posY, variableName, variableObj){

 GUI.Box(Rect (10+(posX*95),10+(posY*85),90,55), variableName);
 if (GUI.RepeatButton (Rect (25+(posX*95), 33+(posY*85), 25, 25), "-")) {
     variableObj -= 0.1;
 }   
 if (GUI.RepeatButton (Rect (60+(posX*95), 33+(posY*85), 25, 25), "+")) {
     variableObj += 0.1;
 }   

 GUI.Box(Rect (10+(posX*95),68+(posY*85),90,22), variableObj.ToString());

}

...and when i click button "-" or "+" nothing happens. I noticed, then if i will extend code from a function and type variables by hand - everything works! There is a webdemo, if somebody want to see it in action: click here.

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 Eric5h5 · Apr 12, 2010 at 11:28 PM

There is a hackish way of doing this in Javascript if you really want. Since classes are always passed by reference and not by value, you can make a class for your variables (that would normally be passed by value) that you want to be directly affected by functions. The class can work basically the same way as a normal passed-by-value variable, but takes more work to set up and use.

Here's an example that makes a class called RefFloat, that is basically a float, except you can use it like a reference variable. The DoubleButton function will make a button that doubles the value of any RefFloats that are passed in.

class RefFloat { var value : float; function RefFloat (a : float) { value = a; } }

var var1 = new RefFloat(.33); var var2 = new RefFloat(2.0);

function OnGUI () { DoubleButton("Variable 1", var1); DoubleButton("Variable 2", var2); }

function DoubleButton (buttonLabel : String, variableName : RefFloat) { if (GUILayout.Button(buttonLabel + ": " + variableName.value.ToString())) { variableName.value *= 2; } }

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 Rafal Sankowski · Apr 13, 2010 at 12:56 AM 0
Share

Woaw! You're the best! Thank you!

avatar image
1

Answer by Eric5h5 · Apr 12, 2010 at 11:03 PM

Variables like ints and floats (which are passed by value) in functions are local to that function, so when you increase/decrease variableName, it changes it the function only, and the variable you passed in is not affected at all.

To get around that, you can affect the actual variables you're passing in by using reference variables instead of standard local variables. However, while you can use functions that have reference variables in Javascript, you can't make them. For that you need to use C# instead. It's very possible to make that function as a separate C# script, and keep the rest of the script in Javascript, if you're more comfortable with that.

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 Rafal Sankowski · Apr 12, 2010 at 11:11 PM 0
Share

Thank you for that quickly feedback!

If I can't do it by functions (because I don't wanna use C#) - is there any other way? I don't want to copy that much of code.

avatar image Eric5h5 · Apr 12, 2010 at 11:30 PM 0
Share

@Rafal Sankowski: Well, actually there is another way (see my other answer), but frankly it's easier to use C# for this sort of thing. Remember you'd only have to use it for that one function and nothing else.

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

No one has followed this question yet.

Related Questions

How can I combine GUI button with script that is put on different gam objects? 1 Answer

Button on one scene changes variable on next scene 1 Answer

change texture of GuiButton in for loop 1 Answer

Change text gui.button in a for bucle 1 Answer

Change GUI button position in code 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