Getting a score the moment the game starts but not when bumping into obstacle

So I understand the theory of the “Object Hit” script. Anything with the script remains untagged at the start of the game. The main character that has the “Score” script checks if the obstacle does not equal the “Hit” tag. If the obstacle does not have the Hit tag then it raises the score by 1 when you bump into it. This then changes the obstacles tag to Hit, preventing it from being used to gain more points.

Here is my issue though. I have done everything in the lecture and have paused and went back multiple times looking at the code shown in the video. I’ve copied it exactly multiple times. I’ve also went into the lectures resources and found the code on github and copied it for both the Score and Object Hit scripts. Also, I’ve checked the video to see that the scripts are on the right assets. The main character gets the Score script and the Obstacle gets Object Hit. For some reason whenever I start the game it gives me a score the moment the main character touches the plane.

The plane has no scripts on it and as far as I can see the scripts shouldn’t have anything that causes it to interact with the plane. Yet, everytime I start the game it gives me plus 1 score for touching it. I’ve tried making the plane start out as Hit, and that works, however when I bump into the obstacle it does not give me a point for the score. It only changes red.

Something seems fundamentally broken about whatever I did. I haven’t had any issues with the course content up until this point and I’ve read through the other answers here and tried most things and nothing has worked.

What am I missing?

Here is a link to a video I made showing the issue

Hi there!

The reason the Score script is being called by the floor (plane 2) is because Score only checks if the thing it collides with does not have the Hit tag. Plane 2’s tag is set to Untagged which, despite the name, does count as a tag itself. Since Untagged is not Hit it passes the if check.

The reason that the Obstacle turns red is because you have this line twice:

GetComponent<MeshRenderer>().material.color = Color.red;

It’s called once before the if check so it will turn red when it collides with anything at all. When the if check is passed it then turns red again which can’t be seen since it’s the same color.

The last question is a bit of a mystery, I can’t really tell why colliding with the Obstacle doesn’t increase the score. Possibly the tag change in ObjectHit is happening before Score checks the Obstacle’s tag? This doesn’t seem very likely but I’m not sure what else it could be. You could try changing Score’s if statement to this:

if(collision.gameObject.CompareTag("Player"))

CompareTag is a more efficient way to check tags, even if this doesn’t help this current problem it’s still useful to know about.

Is there a chance something else is changing the Obstacle’s tag? Perhaps in the Dropper script?

2 Likes

I believe this is extremely likely. There are 2 scripts processing the same collision; Score and ObjectHit. If ObjectHit runs first, it will change the object’s tag to ‘hit’. Then, Score runs and the tag is now ‘hit’, so the score doesn’t increment. It’s what we call a race condition.

2 Likes

Hi CaptainCortisol,

As you and bixarrio wrote, the problem is very likely the execution order of method of the same name, which is random in Unity. All OnCollisionEnter methods get executed in a random order.

To solve this problem, rename the OnCollisionEnter which is supposed to get called last to OnCollisionExit. OnCollisionEnter gets executed after all OnCollisionEnter were executed.

Did this fix it?


See also:

2 Likes

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

Privacy & Terms