Obstacles keep counting hits

Hey guys,

So I’m following along with the tutorial but making some minor changes as I go and I’m wondering if one of those has come back to bite me?

Earlier in the tutorials, my player kept counting hitting the plane. So I fixed that by making a piece of code to not count “Ground” and tagged the plane as ground. I figured instead of changing the obstacles to “Hit” on contact with player, I’d change them to “Ground”. But it doesn’t work. The tags are changed to Ground, but they still count as hits. Even though the plane doesn’t.


I even tried a more complex code to try and count them as “Hit” separately.


After that I tried changing the planes tag to “Hit”, and losing all mention of “Ground” and that completely broke it. It started counting the first touch of the ground as a hit, and all the obstacles.

So, now I’m out of ideas. There must be something simple I’m overlooking.

1 Like

Hi Jhanis,

It’s great to see that you are challenging yourself. :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Regarding the increasing hits value, rename OnCollisionEnter to OnCollisionExit in the ObjectHit class. Or have you already tried that? Also use Debug.Logs to see what’s going on at runtime. Sometimes, there are very obvious flaws in the logic which are not that obvious when reading the code.

Hope this helps. :slight_smile:


See also:

Hi Nina,

Thank you. I’ve been enjoying it so far, but this bit is getting me frustrated, I’ll be honest. Haha.

Oh, sorry, I didn’t realise. I’ll just post the code.

public class Scorer : MonoBehaviour
{

    int hits = 0;

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.name != "Ground") 
        {
            hits++;
            Debug.Log("You Crashed " + hits + " times");
        }
        //hits++; //hits = hits + 1
       // Debug.Log("You've crashed " + hits);
    }
}
public class ObjectHit : MonoBehaviour
{
    private void OnCollisionExit(Collision other)
    {
        if(other.gameObject.tag == "Player")
        {
            //Debug.Log("Bumped into a wall");
            GetComponent<MeshRenderer>().material.color = Color.red;
            gameObject.tag = "Ground";
        }
    }

}

As you suggested I tried swapping OnColisionEnter to OnColisionExit, but it doesn’t seem to have solved it. Should I use OnColisionExit in my Scorer.cs too?

Where would I find the Debug.Logs? All I know is how to use it to print to the console. Like Debug.Log(“Bumped into a wall”);

First of all, use the Debug.Logs you already added to your methods. Don’t comment them out. They were a good idea and will help us understand what’s going on at runtime. In many cases, the code makes perfect sense when we read it but does not work as we think it works. That’s not because we are stupid but because Unity is so complex, that we cannot rely on our logic. Testing and gathering information is crucial.

My idea was that your player bumps into an obstacle. OnCollisionEnter gets executed first. Does it get execute? Who knows. Then OnCollisionExit gets called. Does the if-block get executed? Who knows. As aforementioned, your code makes sense but we cannot know if Unity does what we think it does. You have to check if the if-conditions get evaluated as expected.

You could, for example, log the ‘other’ tag in ObjectHit into your console: Debug.Log("other.tag in ObjectHit: " + other.gameObject.tag);. Maybe your player does not have the ‘Player’ tag assigned. Maybe the tag is spelt ‘player’ or 'Player ’ (with a space). Or something else collided with the obstacle.

You are comparing the name instead of the tag. The name is not the tag.

1 Like

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

Privacy & Terms