- Home /
Destroy GameObject With Collision Using C#
Hi, Im trying to make my player destroy a box when collides with it. Here's what I have so far:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { private GameObject objPlayer; private GameObject objBox;
void Start () {
objPlayer = (GameObject) GameObject.FindWithTag ("Player");
objBox = (GameObject) GameObject.FindWithTag ("Finish");
}
// Update is called once per frame
void Update () {
if ( )
{
removeMe();
}
}
void removeMe () // removes the character
{
Destroy(gameObject);
}
}
What do i need to put in the if statement to make my Player destroy the Box. I would appreciate any help you can give me as i have no idea what i am doing. Thanks in advance!
Did you even search? There's like a 100 posts about destroying stuff. http://answers.unity3d.com/search?q=destroy. Use Destroy(). If you want collision there are also like a 100 posts about collision. http://answers.unity3d.com/questions/tagged/collision. You need to set up colliders on your objects and you can use the physics, meaning you may need rigid bodies, or triggers (read the manual). With colliders, you can either raycast on your own or implement the built-in OnCollisionEnter or OnTriggerEnter methods.
Answer by DaveA · Aug 24, 2010 at 05:18 AM
I think you'll want to restructure you code a little to use colliders. See these links for starters:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html
http://answers.unity3d.com/questions/4063/ontriggerenter-question
And what skovacs1 said.
Having added the collider, you can use the OnTriggerEnter event (rather than Update event)
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroying a prefab on collision with a cube? 1 Answer
Delete 1 col.gameObject and not both 1 Answer
How do I make objects disappear as they are touched? 1 Answer
I can't get Destroy(collision.gameObject) to work? 2 Answers