Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This question was closed May 29, 2019 at 02:08 AM by GraviterX for the following reason:

Other

avatar image
-1
Question by GraviterX · Aug 09, 2015 at 08:57 PM · javascriptbooleandoor

Door Script

Hey guys. I have this door script that is supposed to switch between 2 booleans in order to allow the player to open and close the door. Once the door is opened, it switches the openable boolean to the closeable boolean. However, the door opens just fine, and the closeable boolean activates. However, when I press the mouse down again nothing happens. Can OnMouseDown only be used once? I'm not sure what the problem is. Any advice?

 var closeAnimationFile : String = "door_close_1";
 var openSound : AudioClip;
 var closeSound : AudioClip;
 var inRange : boolean;
 var openActive : boolean;
 var closeActive : boolean;
 
 function Start() {
     openActive = true;
     closeActive = false;
 }
 
 function OnTriggerEnter() {
     inRange = true;
 }
 
 function OnTriggerExit() {
     inRange = false;
 }
 
 function OnMouseDown() {
     if (openActive==true && inRange == true)
     {
         animation.Play(openAnimationFile);
         audio.clip = openSound;
         audio.Play();
         Debug.Log("Open");
         Open();
     }
     if (openActive==false && inRange == true)
     {
         return;
     }
     if (closeActive==true && inRange == true)
     {
         animation.Play(closeAnimationFile);
         audio.clip = closeSound;
         audio.Play();
         Debug.Log("Close");
         Close();
     }
     if (closeActive==false && inRange == true)
     {
         return;
     }
 }
 
 function Open() {
     openActive = false;
     closeActive = true;
     Debug.Log("Set Close Active");
     return;
 }
 
 function Close() {
     openActive = true;
     closeActive = false;
     Debug.Log("Set Open Active");
     return;
 }
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 getyour411 · Aug 09, 2015 at 09:54 PM 0
Share

There's nothing unique about this vs. the countless other Door questions. There's no need to put "return;" at the end of function like Open, Close.

avatar image GraviterX · Aug 09, 2015 at 11:43 PM 0
Share

I tried and that doesn't work either. I'm able to open the door and the boolean closeActive enables, but it won't let me do anything else.

avatar image Hexer · Aug 10, 2015 at 12:00 AM 0
Share

I have this problem as well sometimes when trying to call multiple if-statements in the On$$anonymous$$ouseDown() most of the time it then would only execute the first if statement.

You can work around this with using the else statement. So if openActive is not true, it would execute the code after the else statement. This way you can also delete 1 of your 2 booleans because you don't need 2, if openActive is false then it means it is not open and thus it is closed. No need for the second boolean.

avatar image GraviterX · Aug 10, 2015 at 12:30 AM 0
Share

Thanks @Hexer. This has been bugging me for like a week now. It works now.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by RLin · Aug 09, 2015 at 09:06 PM

Remove lines 30-33. The return statement terminates calling the rest of the function, so close() is never called.

Comment
Add comment · Show 2 · 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 RLin · Aug 09, 2015 at 09:12 PM 0
Share

Did this work? If it did, mark the question as answered and close it.

avatar image GraviterX · Aug 09, 2015 at 09:33 PM 0
Share

No it didn't

Follow this Question

Answers Answers and Comments

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

Related Questions

Player can't block door 0 Answers

Better way to delay a function for a few seconds? Javascript 1 Answer

How to have a Boolean check as true after multiple other Booleans are marked true 2 Answers

Start Animation in code is not working 1 Answer

A question on distance 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