Tag not working when head hits ground

I’m so confused. :frowning: I’m not using a space after “Ground” in my tag or my code. What other game object aside from the Ground (Closed Sprite Shape) needs to be tagged as “Ground”?. The Closed Sprite Shape making the ground is tagged as “Ground” I also tried changing the code to if(CompareTag(“Ground”) and nothing happens. Just to clarify… you have an example to compare to mine? And you can see the difference? And the difference is a space after “Ground”?

Thank you for trying to explain this. I’ve been stuck on this part of the video for like 14 hours and am on my second rebuild and like 30th rewatch of the videos. I’m losing my mind.

How do I check something at runtime?

GameObject objectToDestroy = GameObject.Find(“Groundj”);

Like this? ^

Is this the proper use of other.CompareTag(“Ground”) ?

I get this error
Assets\Scripts\CrashDetector.cs(9,35): error CS1002: ; expected

When using…

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CrashDetector : MonoBehaviour

{

void OnTriggerCollider2D(Collider2D other)

{

    other.CompareTag("Ground")

    {

        Debug.Log("Ouch");

    }

}    

}

With Debug.Logs. :wink:

What I meant by “check something at runtime” is that you log values into your console. For instance, just because your program executes GameObject objectToDestroy = GameObject.Find(“Groundj”); does not mean that objectToDestroy references an object after this line. That line of code might be perfectly fine but it might be that the Find method cannot find any game object named “Groundj” at that moment when it gets executed. And when does the line get executed? After you clicked the Play button. At runtime.

The same applies to the trigger method. Nice method but we cannot know if it gets executed at runtime. For this reason, logging a message into our console could help us learn what’s going on at runtime.

Here is an example:

void OnTriggerEnter2D(Collider2D other) 
{
    Debug.Log("OnTriggerEnter2D was called.");

    // the rest of the code
}

If the message does not appear in your console, you know that OnTriggerEnter2D has not been called. In that case, you don’t have to waste your time with the if-statement because the if-condition does not get checked if the method does not get called.

Did this make sense?

If so, try to figure out what works and what does not work. A few “simple” Debug.Logs with meaningful messages should be sufficient.


Regarding CompareTag: if (other.CompareTag("Ground")) { }. You want use an if-statement, thus you need the if. Inside the parentheses, a boolean expression is required. The CompareTag method returns either true or false.

Alternatively, you could write:

bool correctTag = other.CompareTag("Ground");

if (correctTag)
{ 
    // your code
}

Bear in mind, though, that bixarrio’s suggestion was a suggestion for improving the performance of your code. CompareTag will not solve your actual problem. For this reason, it’s better to focus on your problem with the OnTrigger* method for now. You can change your code (if you want) once the problem is fixed.

1 Like

It is not the correct usage. other.CompareTag("Ground") is a function that essentially does the same thing as other.tag == "Ground" but it also checks if the tag you specify (Ground) is in the list of tags, and it will throw an exception if it isn’t.

The correct usage is

if (other.CompareTag("Ground"))
{
    Debug.Log("Ouch!");
}

But what Nina says is correct: If the OnTriggerEnter2D method is not being called, then there is no reason to keep looking at the ‘if’ statement. You need to first determine if the method is being called.

If it is not being called, then we need to determine ‘why?’. Is it attached to the player? Does the player have a trigger collider that will call that method?


I used his code and successfully solved the problem, but in fact, I’m still a little confused about it. If you understand, please tell me

1 Like

Hi @_kk,

What exactly is confusing you? What would you like to have cleared up for you?

To be able to explain why something works/ does not work, it is also important to provide context. The playerHead variable cannot have solved your problem because if the first part of the if-conditon gets evaluated to false, the rest will not get checked.

Given I understood MrFool’s problem correctly, the if-block does not get executed. For this reason, an additional condition will not fix it.

Okay so ran Debug.Log(“OnTriggerEnter2D was called.”) and it showed up in the console but every if-statement I try to run after that doesn’t show up. It doesn’t make sense to me that the teacher is using 2 colliders on his parent object, the board and the head collider. the board is always touching the ground for the most part. but how does the script know the difference between the 2 colliders he’s using that are on the same object? It clearly works for him but, his script doesn’t seem to specify that the circle collider is the one that causes the trigger. His script just has a Debug.Log for OnTriggerEnter2d. and magically it knows to use the circle collider instead of the capsule collider when hitting the ground?? KK’s example kind of makes sense to me because it specifies the head tag and the circle collider, or at least appears to my code noob perspective. But I don’t understand how the teacher’s script is doing the same thing with way shorter script and no specification on a head tag or CircleCollider2D

My finish line Debug.Log works fine

also, has anyone experienced crashing after hitting ctrl-z multiple times? I basically can’t ctrl-z without a crash. Even with today’s version update.

There’s a difference between a collider and a trigger. If the board collider is not marked as trigger, and the head collider is, then it’s simple: The board will not call the OnTriggerEnter2D method, and the head will not call the OnColliderEnter2D method.

If you can, zip and upload your project somewhere and I will take a quick look at it. Don’t include the Library folder in the zip

Never seen this before. Sorry.

I cheated a fix which was to duplicate the circle collider and set on as trigger and one as not and the Debug works. But that’s not how it’s supposed to be done right? It is debugging properly and i was able to move on. At particles now with no issues

1 Like

I think the issue is I can’t figure out how to make a collider a trigger and actually collide with other colliders at the same time without duplicating the collider.

Thank you, guys, for all the feedback, I really appreciate the help!

I realize now that the teacher never intended the head to hit the ground and collide. Only Trigger. SO, if you want the boarders head to collide with the ground AND trigger it. duplicate the circle collider and have one ON trigger and one OFF.

Yes, exactly. The head in Rick’s game is a trigger collider, which is not supposed to bounce off. For some unknown reason, it does bounce. We suspect that this effect might have been caused by the SurfaceEffector2D component even though it was not supposed to happen.

If you prefer a bouncing head, you could disable “Is Trigger” in the Collider2D component. In that case, you must not use OnTriggerEnter2D, though, but OnCollisionEnter2D. OnTriggerEnter2D gets called only if one of the two colliders in the current collision event is a trigger collider.

It might also make sense to keep the CrashDetector script on the head game object, so Barry’s snowboard collider will not call this method as well.

However, if you found an alternative solution that’s perfectly fine. In many cases, there are multiple ways to make something work in Unity.

Does your game work now as expected? :slight_smile:


See also:

This is making a little more sense now, thank you! Yes, I was able to wrap up the snowboard game finally! I would love to figure out how add a sliding SFX when the board touches the ground. I tried three different ideas i had in the script but no luck. For now, moving on to the quiz game. But, I will definitely revisit the Snowboard game when i get a better understanding of the whole process. Been a blast so far, even cried once, lol

Thanks guys!

I do not know what you’ve already tried but the problem with the current setup is the following: The snowboard collider moves on/above the edge collider. Each bump causes it to “exit” that edge collider.

To figure out if that’s a problem, you could call the OnCollisionStay2D or OnCollisionExit2D method to do what you want to do. Displaying particles should not be difficult. Just call the Play() method on the particle system and call Stop() if you want to stop the emission.

If the situation I described in the first paragraph causes unpleasant results, you could create a new collider: this time, a trigger collider. That trigger collider should be below the snowboard collider. The edge collider of the ground should be inside that trigger collider. Make it large enough so it does not exit the edge collider just because there was a tiny bump. Of course, you will have to use the OnTrigger* methods here instead of the OnCollision* methods.

No worries if this sounds too confusing or difficult. After you completed this course, you should have enough knowledge to make at least this idea work. The main problem is not the code itself but “Unity”. You cannot expect too much precision in the physics simulation, so you will have to fake certain things to achieve nice results. Your future players will just see the result. For this reason, it is perfectly fine to implement solutions that would not work in the real world. :slight_smile:

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

Privacy & Terms