- Home /
Question by
MehmetOguzDerin · Sep 04, 2013 at 04:57 AM ·
c#collisioncollider
Call a Void on Collision
Hello, I have a script that should work for calling Die void. Code:
public GameObject[] enemies;
void Update()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy");
}
void OnCollisionEnter(Collision collision)
{
if(collision.enemies)
{
Die();
}
}
Unity says "UnityEngine.Collision does not contain a definiton for 'enemies/GameObject'". And it is a mesh collider.
Comment
Best Answer
Answer by MehmetOguzDerin · Sep 04, 2013 at 06:35 AM
Solution: OnCollisionEnter doesn't works with Character Controller. OnTriggerEnter isn't good. So i found the ControllerColliderHit.
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.tag == "Enemy")
{
Die ();
}
}
You should look into how C#'s type system works. I know you've got this solved, but your original problem seemed to occur because you didn't understand how types work - it's definitely something you should look up, because it WILL cause problems later.
Answer by DaveA · Sep 04, 2013 at 05:00 AM
public GameObject[] enemies;
void Start()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy"); // but you don't need this
}
void OnCollisionEnter(Collision collision)
{
if(collision.tag == "Enemy")
{
Die();
}
}
UnityEngine.Collision does not contain a definition for 'tag' :(