Hi Nina,
Can’t figure out what I’m missing.
Hi,
There is an if-statement in your code. And the Debug.Log is inside the if-block. Assuming the method gets called (you have to check that at runtime), you have to check if the other game object has got the “Ground” tag assigned. Double check the spelling. The “Ground” tag must not be named “Ground⍽” in Unity. ⍽ is supposed to be a space character in my example.
OnTrigger* methods get called only if a trigger collider is involved in the current collision event.
Also learn to use other.CompareTag("Ground")
instead of other.tag == "Ground"
. If you misspelled the tag, CompareTag(string)
will let you know that the tag does not exist, while tag == string
will just say ‘no, they don’t match’ and continue on it’s merry way
To avoid any misunderstandings: That’s not MrFool’s fault. Rick wrote this code. And since this is “only” the second project in the course, Rick did not want to introduce any new concepts but wanted to show how to write comparisons.
You are right, though, @bixarrio. CompareTag is better than a direct comparison between strings. However, Rick didn’t teach that in this course.
I’m so confused. 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.
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.
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?
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
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.