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
1
Question by Minay · Aug 22, 2014 at 02:23 PM · switchifelse

Problem with On/Off Switch

Hey, I am implementing an on/off switch using the code below. The problem is that the switch gets off but it doesnt get on i.e. it goes inside the inner 'if' but it doesnt go inside the 'else'. Any ideas?

             if (GUI.Button(new Rect(145, 50, 36, 30), "")) //Music
     {
                  if(isOn_M)
         {
         music = music_off;
         isOn_M = false;
         }
         else  
         {
         music = music_on;
         isOn_M = true;
         }
     }
Comment
Add comment · Show 2
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 tanoshimi · Aug 22, 2014 at 02:28 PM 0
Share

You seem to have an awful lot of code for what looks like a simple boolean toggle. Can you not just use:

 if (GUI.Button(new Rect(145, 50, 36, 30), "")) //$$anonymous$$usic
 {
     music = !music;
 }

?

avatar image Minay · Aug 22, 2014 at 02:54 PM 0
Share

music is just a texture that changes the image of button I am using isOn_$$anonymous$$ as the bool

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jokim · Aug 22, 2014 at 04:37 PM

What about :

 if (GUI.Button(new Rect(145, 50, 36, 30), "")) //Music
 {
     isOn_M = !isOn_M;
 
     if(isOn_M)
         music = music_off;
     else  
         music = music_on;
 }

?

This might sounds obvious, but did you instantiate the boolean beforehand ?

Or even, are you changing it in between the clicks ? is it accessed anywhere else ?


EDIT

Ok, You need to initialise the boolean outside the function.

put this line :

 bool isOn_M = false;

inside the class, but outside of functions

(btw, i'm assuming this is c#, i might be wrong)

The reason being that a variable declared inside a function only exists within that function. once the function is done executing, the variable is "disposed of", to free memory. You need the variable to be part of your class. so that it "lives" as long as your class exists.

I hope i'm being clear.

Comment
Add comment · Show 3 · 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 Minay · Aug 22, 2014 at 05:57 PM 0
Share

I initialized it just above the outer if statement. And no it isnt being accessed anywhere else. I tried your code. It doesnt work. Dont know why :(

avatar image jokim · Aug 22, 2014 at 07:18 PM 0
Share

There, Edited my answer.

If you were instantiating the boolean INSIDE the function, the value is only valid while the function is being executed, once it exits, the value is lost. (and will be instantiated again when it goes back in)

avatar image Minay · Aug 23, 2014 at 09:25 AM 0
Share

Thank you :) GBU

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help with If/else statement. 1 Answer

if,if,if, string cases, and if not, then something else? 1 Answer

String Scanning Switch Statement 1 Answer

How to do multiple 'if' statements for multiple different actions 4 Answers

If/Else Statement resulting in glitched animation. 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