- Home /
Make condition for opening a door (can only be opened when a lever has been activated before)
Hey guys, I'm the basic Unity noob but have to finish a project for school. It's a small game level and what I want to do is to enter a house that I built in Maya. Second thing is that you should only be able to open the door when you first activated a lever. The house is a small cabin with an animation on the door, so it should open when you click the door. The lever also has a small animation that is triggered by clicking on it. Both of it already works with this code:
function Start ()
{
animation.Stop();
}
function Update ()
{
if (Input.GetButtonDown("Fire1")) //check to see if the left mouse was pushed.
{
animation.CrossFade ("OpenDoor"); // if pushed play the animation
}
}
So far so good. Now I have this problem: How can I make the animation only play when the lever is already activated (or in other words when that animation has already played) and otherwise it would give some onscreen text like "the door is locked..." I tried with a boolean now, like leverActivated = true when the animation has played, but I don't get how I can use that boolean in the script for the door now...
I would very much appreciate your help and sorry if I'm asking dumb questions...
Answer by Phillipus · Feb 06, 2015 at 07:53 PM
Try using public variables and changing them from other scripts, e.g. when the lever is activated, it calls a public function in the door's script that sets a boolean as true.
Example (its in C#):
Lever script:
Gameobject door;
void Start() {
door = Gameobject.Find("Door")
}
void ActivateLever() {
door.GetComponent<Script>().SetOpenable(true);
}
Door Script:
bool openable;
void Start() {
animation.Stop();
openable = false;
}
void Update() {
if(Input.GetButtonDown("Fire1") && openable){
animation.CrossFade ("OpenDoor");
} else {
// do something for if the lever isn't pushed
}
}
public void SetOpenable(boolean set) {
openable = set;
}
You would change Door
in Gameobject.Find("Door")
to the name of the door, and Script
in `GetComponent
Your answer
![](https://koobas.hobune.stream/wayback/20220613182644im_/https://answers.unity.com/themes/thub/images/avi.jpg)