- Home /
Destroy() not working on collision
Here is my script:
function Update()
{
transform.position += transform.up * 20 * Time.deltaTime;
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
I do not have the slightest clue. I tried changing tags. They both have rigidbodies. Im shooting a prefrab at it with this:
var bullet:Transform;
function Update()
{
if(Input.GetMouseButtonDown(0))
{
clone = Instantiate(bullet, transform.position , transform.rotation);
Destroy(clone.gameObject,2.0f);
}
}
They both have colliders and rigidbodies. I made sure everything was in place like the tag of enemy and stuff.
It isn't very clear what you are asking for help with. Is the OnTriggerEnter being called? If so, is the tag properly set to Enemy? I doubt you have the bullet tagged with "Enemy" which is what it would need to be tagged with assu$$anonymous$$g the above script is attached to the object that you want to be destroyed when it collides with a bullet.
Unfortunately it isn't very clear what is the problem or what is the expected outcome, which is likely why the initial question was downvoted.
kevork doesnt know what he is talking about. Yes i want to destroy the bullet too but havn't got there yet. I found out the problem. Active Is Trigger on both of them xD
Answer by Piflik · Apr 19, 2012 at 08:25 PM
Have you checked the 'Is Trigger' option in the colliders?
Your code is good, methinks.
OnTriggerEnter needs Triggers, OnCollisionEnter needs Colliders that are not Triggers.
i didnt read ur answer to my comment question above. I figured it out the hard way xD
Answer by rutter · Apr 19, 2012 at 08:27 PM
Are you sure that all of these functions are being called? You might try a few distinctive `Debug.Log()` calls to watch your code's execution -- say, log each time you create a bullet, each time the bullet hits something, each time you try to destroy the bullet, and so on.
Check what actually happens against your expectations, and you should have a better idea of what's going wrong.
You could check the script reference for `OnTriggerEnter()` and make sure that you have things set up right. You mention that both objects have rigidbodies attached. Do they have colliders? What layer are those colliders on? Is one or more of the colliders a trigger?
Your answer
Follow this Question
Related Questions
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer
Destroying object when player walks over it 1 Answer
How to destroy an instantiated prefab object and keep instantiating it 1 Answer
destroy instantiated game object on collision with another game object 1 Answer
Collision Problem 1 Answer