x


Toggle GUI button

How does it work the GUI toggle?

I want to be able to switch between two textures if the toggle is on or off.

more ▼

asked Mar 19 '10 at 11:10 AM

maveryck21 gravatar image

maveryck21
239 29 32 44

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The GUI.Toggle is described on this page in the manual and here in the scripting reference.

If you want something to happen when toggling the toggle, here is one way to do it:

var toggleBool = true;

function OnGUI () {
    var toggleBoolNew = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Toggle");

    // Check if the toggle was toggled
    if (toggleBoolNew != toggleBool) {
        if (toggleBoolNew == true)
            Debug.Log("Toggle was enabled");
        else
            Debug.Log("Toggle was disabled");

        toggleBool = toggleBoolNew;
    }
}
more ▼

answered Mar 19 '10 at 11:21 AM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

Thanks..now I understand how to activate functions regarding the on/off state of the toggle

Mar 19 '10 at 12:16 PM maveryck21

== true could be omitted and consider adding the curly brackets {} even if you have only one line to execute.. because usually you don't have only one.. for example.. what if you want to add the code for changing the texture and see Debug.Log("Texture was changes")..

Mar 19 '10 at 12:34 PM Lipis

@maveryck21 don't forget to upvote awesome answers.. especially the ones that helped you :)

Mar 19 '10 at 12:35 PM Lipis

@Lipis I added == true to make the code more readable - this language construct is not obvious for people new to programming, for example. But you're right that it can be left out. About where to use curlies, that is a matter of taste.

Mar 19 '10 at 01:57 PM runevision ♦♦

@Rune Skovbo Johansen ♦♦

what if I only want to play a sound once using toggle button??because I tried your code and the sound keeps playing...

How do I make it done once??

PROBLEM SOLVED : by adding a conditional statement before setting the boolean to true I'm able to set the toggle to play the sound only once.

Aug 02 '11 at 02:50 AM beco13
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x237
x159

asked: Mar 19 '10 at 11:10 AM

Seen: 5207 times

Last Updated: Oct 17 '12 at 10:06 AM