Expanded the Obstacle Course and now some of my OnCollisionEnter do not work

I have been fiddling around with the obstacle Course from the lecture a little bit and have added a live counter instead of a score. Now I sometimes run into the issue that my player collides with an obstacle, the obstacle changes colour like it should thru the ObjectHit script but my life counter does not decrease. This seems to only be happening with Objects that have a rigid body like the falling blocks or rolling balls. Does anyone have an Idea what might be the issue?
This is my Scorer script:

public class Scorer : MonoBehaviour {
    public int lifes = 3;
    public GameObject Button;
    private void OnCollisionEnter(Collision collision) {
       if (collision.gameObject.CompareTag("Collider")) {
            lifes--;
            if (lifes == 0) {
                GetComponent<Mover>().enabled = false;
                Button.SetActive(true);
            }
       }
    }
}

Hi @Domenixius,

Welcome to our community! :slight_smile:

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? To which game object did you attach the Scorer script?

Hey,
Thanks! :slight_smile:

The Scorer script is connected to the player.
I think I found a fix. In the ObjectHit script that was attached to the obstacles, the tag of that object is changed to “Hit”. The Scorer script only reacts to Objects with the Tag “Collider”. I guess that the ObjectHit script reacted before the Scorer script could act thus changing the Tag to “Hit” and the Scorer script not activating anymore.

That sounds like the problem/solution. Since your code is checking for the “Collider” tag, the other game object needs this specific tag. Otherwise, the if-block will not get executed.


See also:

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

Privacy & Terms