- Home /
Using If functions to open door with a key.
Hi everyone, I'm having a little trouble with my scripting and can't see why this isnt working.
I'm trying to have a script where you pickup a key and use it to open a door.
This is what i'm currently using. Heres the code on the key
function OnTriggerEnter () {
Debug.Log("hit key");
gameObject.Find("KeyDoor").SendMessage("HitKey");
}
And heres the door.
var key : boolean =false;
function OnCollisonEnter (Other : Collision){
if (key == true){
Destroy(gameObject);
}
}
function HitKey (){
Debug.Log("Message Received");
key = true;
}
Right now when i run into the key the game ends its self. Any help would be greatly appreciated!
For starters, the function you're looking for is named OnCollisionEnter, not OnCollisonEnter. Correct spelling is crucial in these cases.
"This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached"
Check if you have the colliders and rigidbodies setup properly .
You could be unlucky and the doors collider is triggered first, and then the keys collider. You should only need one OnCollisionEnter for this. You could set the:
Destroy(gameObject);
in the Hit$$anonymous$$ey function. This way you could remove the doors OnCollisonEnter.
Answer by fafase · Jul 10, 2012 at 03:59 PM
Few things are going wrong:
You are missing the parameter.
function OnTriggerEnter(other:Collider)
Is the door a child of the player? Because the command below is looking through the hierarchy of the game object the script is attached to.
gameObject.Find("KeyDoor").SendMessage("HitKey");
I think you meant: gameObject.Find("KeyDoor").SendMessage("HitKey");
As mentioned in comment:
OnCollisonEnter=>OnCollision (Other : Collision){
The way it is done now is not really making sense, your collision functions needs to check what is going on and what is colliding/
if(other.gameObject.tag=="Player")
What you have now is that when collecting the key, you send the message that turns key to true and if the door is colliding with anything, you destroy the door. Your door is probably cooliding with a wall or else.
Now a solution:
function OnTriggerEnter (other:Collider) {
Debug.Log("hit key");
if(other.gameObject.tag =="Player")
GameObject.Find("KeyDoor").SendMessage("HitKey");
}
And heres the door.
var key : boolean =false;
function OnCollisonEnter (Other : Collision){
if(other.gameObject.tag=="Player"){
if (key == true){
Destroy(gameObject);
GameObject.Find("Key").SendMessage("DestroyKey"); //If you also want to destroy the key
}
}
}
function HitKey (){
Debug.Log("Message Received");
key = true;
}
Your player hits the key, and when he reaches the door, the door checks if the player got the key. If the player has it, then the door destroys. I would guess you want to destroy the key too so add:
function DestroyKey(){
Destroy(gameObject);
}
Your answer
Follow this Question
Related Questions
Press an in-game switch to open a door - help? 2 Answers
GUI text, door and object collider 2 Answers
Open door with key 1 Answer
Make doors open with different keys? 1 Answer
Opening a door with a key 2 Answers