- Home /
Question by
de2truction · Jun 19, 2016 at 08:29 AM ·
javascriptdestroydoorkey
Key to open door not working.
I've been trying for too long; can someone help?
#pragma strict
var Key : GameObject;
var sound : AudioClip;
public var LevelName ="My level name";
var Unlocked : boolean = false;
if (Key == null) {
Unlocked = true;
}
function OnTriggerEnter () {
if(Unlocked == false){
print("The door is locked.");
}
else{
GetComponent.<AudioSource>().PlayOneShot(sound);
yield WaitForSeconds(1);
transform.Rotate(0,90,0);
yield WaitForSeconds(1);
Application.LoadLevel(LevelName);
}
}
I have a code so that when I touch a key, it gets destroyed. This script is meant to unlock the door when the key is destroyed, but I can't seem to change the variable ingame.
I used:
if (Key == null) {
Unlocked = true;
}
but the unlocked variable's checkbox just doesn't check.
Thank you!
Comment
Answer by jdean300 · Jun 19, 2016 at 08:38 AM
I don't know much about JavaScript within Unity, but it is looking like you need to have that if statement inside of your Update function:
function Update(){
if (Key == null) {
Unlocked = true;
}
}
Otherwise your if statement is only going to run one time at the start of the game.