x


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!

more ▼

asked Aug 24 '10 at 04:15 AM

Adam 4 gravatar image

Adam 4
14 2 3 8

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.

Aug 24 '10 at 04:34 AM skovacs1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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)

more ▼

answered Aug 24 '10 at 05:18 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x2172
x785

asked: Aug 24 '10 at 04:15 AM

Seen: 8975 times

Last Updated: Aug 24 '10 at 04:15 AM