Tags returning true before collision

Hi!
So, just a simple and quick question that I can’t find an answer to. And yes I’ve tried searching the answers and the forums but didn’t find anything related.

When I’m using tags to check if a enemy is colliding with a waypoint, and also when I try to check if my player is colliding with a wall, the if-statement returns true even if there’s no collision with a collider with that tag. Even if the game object with the tag is more than 3 units away, and thus not colliding with the other object, the statement still returns true.

So I guess my question(s) is:

What could be causing this?

Is anyone else experiencing this problem?

Is there a solution to this?

Thanks in advance!

Erik

Edit:

For those of you who wanted the code:

Player code:

    function OnCollisionStay (col : Collision)
    {
    	if(col.gameObject.FindWithTag("Terrain"))
    	{
    		canJump = true;
    	}
    	
    	else
    	{
    		canJump = false;
    	}
    }

Enemy code:

  function OnCollisionEnter (col : Collision)
    {
    	if (col.gameObject.FindWithTag ("Waypoint"))
    	{
    //code for switching walking direction
        }
    }

And yes, both the player and the enemy are colliding with something else, the ground (“Terrain”). BUT, as they both are colliding with the terrain as soon as I start the game and the statement doesn’t return true until a certain position in the game, I don’t think that this is the issue. If it was, then the statement would return true as soon as I started the game (i.e. as soon as they hit the ground).

If you want to check the collider tag, try CompareTag:
col.gameObject.CompareTag(“YourTag”)

Looking at FindWithTag description:

static function FindWithTag (tag : String) : GameObject
Description
Returns one active GameObject tagged tag. Returns null if no GameObject was found.

So your code was returning any object with that tag, disregard of collision. This is because the gameObject attached to collider inherits the GameObject member functions, including FindWithTag.

The expression

col.gameObject.FindWithTag("Tagname")

is not doing what you think it’s doing. If you look in Unity’s script reference for GameObject, you’ll see that FindWithTag is a “class function,” meaning it isn’t specific to any specific game object; instead it finds any object in your scene with that tag.

In order words, it’s equivalent to calling the function via the class (i.e. GameObject.FindWithTag("Tagname"), without any specific instance.

What you want is:

col.gameObject.tag == "Tagname"

i.e. checking the tag of the specific game object you collided with.