1
votes

I've made many colliders before and that I used was (collider otherOnTriggerEnter(collider other)) and then check if the other collider is the one I want by comparing TAG. But this time I'm not triggering the canMelee boolean. My code:

ArrayList Enemy;
public StatData stat;

[SerializeField]
private bool canMelee = false;

/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider col)
{
    if (col.tag == "Enemy")
    {
        Debug.Log("Now you can do melee attack!");
        canMelee = true;
    }
}

So I wrote this code but somehow this doesn't work I checked the tag and added it to the player game object but it still doesn't work.

I tried to add another debug to check if OnTriggerEnter() is happening just above the if branch : Debug.Log("Triggered"); and it gets triggered.

I can't just figure put what's wrong. Please help me.

2
Have you tried debugging and placing a breakpoint on the if block? You can then check what the tag of "col" is by inspecting it. I suspect that maybe you have set the parent gameobject tag or something similar. Could you maybe post a screenshot of having the object selected? - Immorality
Can't tell you whats wrong, there are several things can cause this issue(And several others like it). We need to know your GameObjects Components. Include a screenshot of the 2 GameObjects Inspector. Including their layers and tags. - AresCaelum
Also have you tried printing out col.tag instead of "Triggered". - AresCaelum

2 Answers

2
votes

First double check that your enemy game objects have the tag "Enemy". Next make sure that your player collider has the "Is Trigger" option selected in the Unity inspector.

Next try using col.gameObject.CompareTag("Enemy") instead of: col.tag == "Enemy" in your if statement.

Check out the Unity documentation as well on Colliders: Unity Collider documentation Unity Component.CompareTag method with example

Hope this helps!

0
votes

The first advice I can give you, is ALWAYS, when writting a code Debug it, just make always sure that every step in your code is beeing executed. Inside every if just debug what it is supposed to check and if its true, inside your void, make sure its running the void, etc...

Next: -Check if you have your object tagged as "Enemy", this can be on of the most common errors, and its so easy to solve, you may have written it wrong, or just not set it up. -Here is the trigger documentationdocumentation. -After this, make sure your gameobject is market as triggered.

-If you want you can use also colliders, and this also shows you how to check the tag.

Make sure you set all of this up, if this doesn't helped you, comment bellow, with prints, your gameobjects, codes, etc related to this.