The dropper object is not counting in the score

My code is exactly the same as shown in the video. When my player hits the “dropper” object it doesn’t count in the score log. I noticed that for all other objects in my scene the Score script is being called before the Score object, but it doesn’t happen for the dropper, it calls the ObjectHit first, and as the Hit tag is applied in this script the score doesn’t count. What should I do?

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Make sure the method names are spelt the same way as Rick’s. One wrong character, and it might be that the method does not get called.

Are there any error messages in your console during runtime when the score is supposed to increase?

I got this error too. My score is not going up, but all the code is exactly the same I think. It seems that the objects are getting the tag “Hit” just before the score script so that it does not increase the score.

Hi Flash,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Yes,

It seems that the tags of the objects do get changed to “Hit” when they interact with the player, but the score does not update.

That’s a good observation. Given you tested that with the Debug.Logs, the output indicates that there is a problem with the execution order. In our ObjectHit and Scorer classes, we have a OnCollisionEnter each. If the method of the Scorer object gets executed first, if (other.gameObject.tag != "Hit") gets evaluated to false because the other game object does not have the “Hit” tag yet. In Unity, the execution order of scripts is arbitrary, so this problem is not caused because you did something wrong.

There are two ways how you could solve this problem:

  1. You add both the Scorer and the ObjectHit scripts to the list in Edit > Project Settings > Script Execution Order. The numbers are meaningless outside the context of that list and do not represent any time in second. Just make sure that ObjectHit comes before Scorer, so ObjectHit gets executed before Scorer.

  2. Instead of OnCollisionEnter, you use OnCollisionExit in the Scorer class. Since OnCollisionExit gets executed after OnCollisionEnter, that would solve the problem.

Did that help?


See also:

This did not seem to have worked either, but I have now tried putting all the code into one script, and this seems to have worked. I just gave the Plane a “NonObstacle” tag.

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag != "Hit")
        {
            if (other.gameObject.tag != "NonObstacle")
            {
                hits = hits + 1;
                other.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
                other.gameObject.tag = "Hit";
                Debug.Log("You have bumped into something " + hits + " times.");
            }
        }
    }
2 Likes

Good job. :slight_smile:

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

Privacy & Terms