x


Toggle button problem

I have my code right here:

private var toggleState1 : boolean = false;

function OnGUI() { toggleState1 = GUI.Toggle(Rect(430, 210, 100, 30), toggleState1, "Sound on/off"); AudioListener.pause = toggleState1; }

It works fine when toggled, but when I go back to my main menu it is untoggled. How can I save that it is toggled in the whole game?

more ▼

asked Sep 11 '12 at 07:13 PM

jennes gravatar image

jennes
4 1 4 8

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

1 answer: sort voted first

Two options:

http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

This will prevent the gameObject from being destroyed when you change scenes. There is a draw back. If you go back to the scene where this gameobject is placed you can create multiple instances of it. They will also stay persistent throughout the game so you need to place code to check to see if the gameobject already extists.

or

http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

This will stay persistent even if you quit the game. Its very easy to use and straight forward.

Hope this helps.

more ▼

answered Sep 11 '12 at 08:05 PM

PAEvenson gravatar image

PAEvenson
2.1k 2 4

I want to use DontDestroyOnLoad, but when I use that, the whole button will be loaded in all scenes. I only need if the button is toggled or not. Maybe I should try PlayerPrefs but i'm not sure how to deal with that..

Sep 11 '12 at 09:29 PM jennes

Got it! thanks!

Sep 19 '12 at 12:28 PM jennes
(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:

x242

asked: Sep 11 '12 at 07:13 PM

Seen: 206 times

Last Updated: Sep 19 '12 at 12:28 PM