• 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 Edyvargas · Oct 28, 2011 at 08:41 AM · onmousedown

OnMouseDown inside if statment

Hi, i have a script to open and close a animated door on mouse click, the script use OnMouseDown Function to play the open animation when you click on the door collider, the problem its when you want to play the close animation clicking on the same (but) opened door, i cant use OnMouseDown Function twice on the same script, i try to create a "if" statment to activate the OnMouseDown Function based on a new variable that changes if the door is opened or if its closed, but i cant put the functions inside "if" statments.

Anyone perhaps have an idea on how to do this?

Thanks!

this is part of the code:

 function OnMouseDown () {
 
 doorObject.animation.Play ("DoorOpen");
 
 audio.PlayOneShot(sound1);
 
 print("The door is open!");
 
 }
 
 
 // this second OnMouseDown Function gives me an error
 
 function OnMouseDown () {
 
 doorObject.animation.Play ("DoorClose");
 
 audio.PlayOneShot(sound2);
 
 print("The door is close!");
 
 } 

I dont use "Input.GetMouseButtonDown (0)", beacuse i want the animation plays only when you click "over" the door...

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 oxium · Oct 28, 2011 at 08:48 AM 0
Share

why do you have 2 On$$anonymous$$ouseDown functions ?

wouldn't you use On$$anonymous$$ouseUp ins$$anonymous$$d ?

http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.html

avatar image Edyvargas · Oct 28, 2011 at 08:53 AM 0
Share

..beacuse i want the door to stay opened until i hit on it again with another click...(in this case its a realworld kind of door, not the usual slider door), thanks for the answer, the link has interesting info..

2 Replies

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

Answer by gfr · Oct 28, 2011 at 08:47 AM

You can have only one event handler in your class. Instead you can use a flag to remember the door state:

var doorOpen = false;

function OnMouseDown () { if (!doorOpen) { // ... open door } else { // ... close door } doorOpen = !doorOpen; }

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 Edyvargas · Oct 28, 2011 at 09:02 AM 0
Share

THAN$$anonymous$$ YOU, it works very good, im still learning Unity scripting and dont know exactly what is a flag, but it WOR$$anonymous$$S!

avatar image gfr · Oct 28, 2011 at 09:22 AM 0
Share

@Edy: Flag is just a term for a variable that's used to remember if something is on/off, enabled/disabled, ...

avatar image Edyvargas · Oct 28, 2011 at 08:40 PM 0
Share

Interesting to know, flags could help a lot on a code, thanks gfr.

avatar image
0

Answer by Edyvargas · Oct 28, 2011 at 09:11 AM

This is the working code if anybody wants to see it complete, thanks to "gfr" and all the people on the Unity forum for the help!

Note: the 3d animated door has 2 animations, one to open the door and the other to close it, the script above must be attached to the door object with collider and audio source components on it, also you must set on the inspector all the variables on the script (animations and sounds), and set your door object name too.

var animation1 : AnimationClip; //var name for animation 1. var animation2 : AnimationClip; //var name for animation 2. var sound1 : AudioClip; //var name for sound 1. var sound2 : AudioClip; //var name for sound 2. private var doorObject : GameObject; var doorName = "Door00"; var doorOpen = false;

function Start() {

doorObject = GameObject.Find(doorName);

}

function OnMouseDown () {

if (!doorOpen) {

     doorObject.animation.Play ("DoorOpen");

     audio.PlayOneShot(sound1);

     print("Open!");

 } else {

     doorObject.animation.Play ("DoorClose");

     audio.PlayOneShot(sound2);

     print("Close!");

 }

 doorOpen = !doorOpen; 

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown with 360 controller unity? 1 Answer

Does onMouseDown convert to a Touch command in iOS or not? 1 Answer

Alternative to OnMouseDown() 2 Answers

Is there something like OnMouseDown which doesn't require a collider on my prefab? 3 Answers

How to apply OnMouseDown on sprites according to z position... 3 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