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
2
Question by 3dDude · Jun 03, 2010 at 07:21 PM · javascriptbooleanswitch

switching booleans

i have this code

var ispaused : boolean = false; function Update () { if(Input.GetKeyDown("p")){ Switch(ispaused); }

}

function Switch (bool){ if(bool == false){ bool = true; } else{ bool = false; } return bool; }

the code is supposed to switch the var ispaused, but it does not. Any help on the matter would be very nice.

Comment
Add comment · Show 1
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 Cyclops · Jun 04, 2010 at 02:33 AM 1
Share

@3dDude, just FYI, you can move the Checkmark to an Answer, even after using it. Consider moving it to @Eric5h5's answer, it really is a better way of solving your problem.

2 Replies

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

Answer by Cyclops · Jun 03, 2010 at 07:34 PM

I'm not that familiar with Javascript, is it passing the Boolean by reference, or value? If it's by value, then you need to say:

ispaused = Switch(ispaused);
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 3dDude · Jun 03, 2010 at 07:39 PM 0
Share

cool that works thanks!

avatar image
8

Answer by Eric5h5 · Jun 03, 2010 at 07:59 PM

You can actually remove the function entirely, and use this instead:

if(Input.GetKeyDown("p")){
    isPaused = !isPaused;
}
Comment
Add comment · Show 6 · 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 equalsequals · Jun 03, 2010 at 08:16 PM 1
Share

efficiency is king.

avatar image Cyclops · Jun 03, 2010 at 08:56 PM 0
Share

Sigh... why do I even bother answering questions? :)

avatar image Eric5h5 · Jun 03, 2010 at 09:28 PM 1
Share

@Cyclops: well, you actually answered the question. ;) I didn't really answer the question, I proposed something entirely different (albeit more efficient)...the correct answer is useful even if the particular function in question isn't in this case.

avatar image Cyclops · Jun 04, 2010 at 02:31 AM 0
Share

@Eric5h5: so - I gave an Answer that was technically correct, yet still managed to be inferior to an Answer that didn't even address the Question. Thanks, I feel better now... :)

avatar image Eric5h5 · Jun 04, 2010 at 03:41 AM 0
Share

@Cyclops: Nah...if 3dDude runs into a similar problem with a completely different function, he'll know what to do because of your answer, so it's not inferior. ;)

Show more comments

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

1 Person is following this question.

avatar image

Related Questions

If...else statement with errors 1 Answer

Camera switch after specific time 1 Answer

Weapon pick up and switching script 2 Answers

Initialize multidimensional boolean array as all false Unity JS 2 Answers

Change Variable on Another Script 2 Answers


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