Destorying objects on collsion
This is my code:
using UnityEngine; using System.Collections;
public class ObjectDestroy : MonoBehaviour {
void OnCollisionEnter(Collision : collision)
{
if (collision.gameObject.tag == "Spell")
{
Destroy(collision.gameObject);
}
}
}
However, what i want too happen is that it will destory the object once anything tagged in spell colides with it but i can't get it working at all...
Answer by Cepheid · Jan 24, 2016 at 02:04 PM
Well for a start you can't use colons in the method parameters this is C# not UnityScript.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Spell")
{
Destroy(collision.gameObject);
}
}
If that isn't the issue, then are you sure that you have tagged the required objects with the "Spell" tag?
Is the actual tag in the game engine spelled with an upper case S?
Does the object you are colliding with have a non-kinematic Rigidbody?
Does your object have a non-kinematic Rigidbody?
Are the colliders of your object or the object you are colliding with marked as isTrigger? In which case maybe you have meant to use the OnTriggerEnter() method.
Are you using the correct physics system? I.E calling OnCollisionEnter() when you mean OnCollisionEnter2D.
That is all the possible issues I can think of. Hope it helps!