OnCollisionEnter(Collision other)

Not really understanding the other part.

public class Scorer : MonoBehaviour

{

    int hits = 0;

   private void OnCollisionEnter(Collision other) 

   {

       if(other.gameObject.tag != "Hit")

       {

            hits++;

            Debug.Log("You've bumped into a thing this many times: " + hits);

       }

   }

}

In the if(other.gameObject.tag != "Hit") I understand why I would use != “Hit” which is pretty much saying if the object I’m hitting doesn’t equal Hit then gain a point and if it does add nothing.
The part that’s confusing me isif(other.gameObject.tag?? I guess I’m not understanding why I would add other there.

When OnCollisionEnter gets called, which it only does when the GameObject the script calling it is attached to collides with something, the method receives information about the collider, which you can access via the “other” variable here. So other.gameObject.tag is the tag on the GameObject the other collider belongs to. If you simply typed in gameObject.tag, you’d be checking the tag on the GameObject the script belongs to instead.

I’m sorry if I’m not being super clear about this. I realize I have trouble explaining sometimes. If i can clarify, let me know.

1 Like

Ah okay. This made sense to me. Thanks :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms