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 war6666 · Jul 31, 2013 at 01:16 PM · if-statementselse

If not working as expected.

I am rather new to coding and I have tried to search for a solution to this problem but I can not find it.

I have a script sending a raycast so that whenever I press the 'E' key it sends the message "use" from that script to this script. This seems to be working as I need it to, however the If-else statement in this script is not doing what I need it to do.

 #pragma strict
 
 var toggledOn : boolean = false;
 var onSound : AudioClip;
 var offSound : AudioClip;
 var toggleSound : AudioSource;
 
 function Start(){
     var aSources = GetComponents(AudioSource);
     toggleSound = aSources[1];
 }
 function use(){
     if (toggledOn == true){
         toggledOn = false;
         audio.PlayOneShot(offSound);
     }
     else {
         toggledOn = true;
         audio.PlayOneShot(onSound);
         toggleSound.Play();
     }
 }

To my understanding (that is obviously flawed), whenever the other script sends the message "use" to this script, it should be then running the if statement, and if the requirement is not met, then the else statement should run. However, when I press "E" all that happens is it runs the audio.PlayOneShot(onSound); toggleSound.Play(); part. It does not appear to be setting the toggledOn variable to true; it only plays the two sounds.

If I set the variable toggledOn to true before running the game, it continues to do the same thing. (It doesn't seem to matter whether or not the variable is set to true or false, each yields the same result.)

Comment
Add comment · Show 4
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 amphoterik · Jul 31, 2013 at 01:17 PM 0
Share

Are you sure offsound and onsound are different sounds?

avatar image war6666 · Jul 31, 2013 at 01:26 PM 0
Share

Yes. Also, the way I understand that this script should work, is that it would change the variable toggledOn to true/false every time I press "e", which it is not doing either.

avatar image roojerry · Jul 31, 2013 at 01:31 PM 1
Share

When checking Input for the keypress "E", do you happen to be using Input.Get$$anonymous$$ey ins$$anonymous$$d of Input.Get$$anonymous$$eyDown? That could cause the "use" function to fire on multiple consecutive frames

avatar image ATMEthan · Jul 31, 2013 at 01:32 PM 0
Share

Is there any other script or code snippet that sets the toggledOn variable? Some other script could be changing it to false.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jul 31, 2013 at 01:40 PM

This code should work, although there's an easier way to toggle a boolean variable. You could add debug lines to your code, like this (boolean toggled in a different manner):

 function use(){
     toggledOn = !toggledOn; // toggle the boolean
     Debug.Log("use called - toggledOn now is "+toggledOn);
     if (toggledOn){ // toggledOn became true
        audio.PlayOneShot(onSound);
        toggleSound.Play();
     } else { // toggledOn became false
        audio.PlayOneShot(offSound);
     }
 }
Comment
Add comment · 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

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

19 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 avatar image avatar image avatar image

Related Questions

if statement for 6 ints help C# 2 Answers

If statement always true 3 Answers

Any Ideas on how to solve this? 1 Answer

Need Help Improving If Else statement 1 Answer

Prevention of air movement 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