- Home /
How do I destroy an object with a trigger?
I'm trying to have a player walk over a pressure plate trigger and have that cause a door to destroy itself, but I can't figure out the code for it. I'm also really new to Unity, and programming in general, so I think I'm trying to do something out of my skill set. The two ways I've tried are:
else if(trigger.name == "PressurePlate6") { Destroy(GameObject.name("Lockout")); } else if(trigger.name == "PressurePlate7") { GameObject("BoulderTrapDoor").isCollider = false; }
Answer by Eudaimonium · May 12, 2015 at 01:04 PM
To what is this script attached, I assume the player?
If you need to destroy an object named "Lockout", make a public variable outside the function, and assign the door's gameobject to it via the Inspector. Example: public GameObject lockoutDoor;
and then within your function: Destroy(lockoutDoor);
Same with the drapdoor object. Use as much Inspector assigning where possible. Otherwise, try to find all the gameObject within your "Start" function, and in Update only access the saved objects.
Answer by itsmealex100 · May 12, 2015 at 12:50 PM
Hi, first off this site is great for getting better with programming you'll be fine after a few months of experience.
Okay, this is how I would do it.
1. Tag your player with "Player". 2. Make sure the player is NOT set to IsTrigger. 3. Put this script on your PressurePlate.
public GameObject door; // This is where you reference the door you want to destroy
void OnCollisionEnter(Collision other) {
if(other.tag == "Player") {
Destroy(door);
Destroy(gameObject); //This will destroy the pressure plate after it has been triggered
}
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Player")) {
Destroy(door);
Destroy(gameObject); //This will destroy the pressure plate after it has been triggered
}
}
Your answer
Follow this Question
Related Questions
Javascript. Respawning a character after destroyobject(). 0 Answers
destroy prefabs 1 Answer
Trouble with upgrading towers 1 Answer
Destroy Clone when colliding with 2d box collider 0 Answers
2d edge collider wont destroy? 0 Answers