- Home /
Other
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;
}
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.
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.
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.
Thanks @Hexer. This has been bugging me for like a week now. It works now.
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.
Did this work? If it did, mark the question as answered and close it.