collision script not working

Why doesnt this work?

function OnCollisionEnter(hit: Collision){
    if(hit.gameObject.tag == "Collectable"){
       print("hit collectable");
    }
}

Put a print statement outside the if block. Maybe something like:

print(“OnCollisionEnter was called. Tag was”+hit.gameObject.tag);

Then you will be able to confirm if the function was called in the first place and what game object tag is there.

You didn’t exactly say what didn’t work … but I will presume that you aren’t seeing output from “hit collectable” (the print statement).

There are a number of possible reasons that the print statement is not being reached:

  1. The object that we collided with does not have the tag “Collectable”
  2. The object that we collided with does not have a collider component associated with it
  3. Our own object does not have a collider associated with it
  4. Our own object does not have a rigidbody component associated with it
  5. One or both of the collider components are tagged as “Triggers”
  6. The script that you are showing is not attached to our object
  7. The script is unchecked (disabled)
  8. No collision actually happens and hence no collision event is thrown