- Home /
Collision not working... [Solved (b/c I'm dumb)]
So I'm making a game where you cook food while defending it against people trying to take your food. Long story short I'm currently working on the people being spawned and I try to shoot them but it doesn't work. I have colliders on both of the objects as well as RigidBodies. They are not marked as triggers and even when my bullet collides with something else it doesn't work (I tested with Debug.Log). Here is my script for my bullet.
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
private void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
private void OnCollisionEnter(Collision col)
{
Debug.Log("Works");
if (col.gameObject.tag == ("Enemy"))
{
Destroy(col.gameObject);
Debug.Log("Works even better");
}
}
}
I don't have an enemy script yet. All of the enemies have that tag too. I tested with triggers and that didn't seem to change anything.
PLS HALP ME!!!!!!
Answer by joemane22 · Mar 09, 2020 at 12:47 AM
Make sure both the enemy and bullets have colliders attached.
OnCollisionEnter will not register if either is set as triggers so make sure both the enemy and the bullet colliders are not marked as triggers.
Make sure your tag is also capitalized.
That's all the problems I could think of that could happen hopefully this helps!
EDIT - One of the problems was you used the Collisions gameobject when you should use col.collider.gameobject to access the object that the current object collided with!
They both have colliders, they are not marked as triggers and the tags are capitalized. Thank you for trying to help. Really appreciate it!
I know what it is, you are checking the collisions tag, you need to check col.collider.gameobject.tag ins$$anonymous$$d. The collision is it's own separate object.