Trigger not detecing right away

so basically this is what i have

var count : float = 0;

function OnTriggerEnter(hit : Collider)
{
	if(hit.gameObject.tag == "Player")
	{
		count++;
		if(count == 1)
		{
			Openlevel("My Game");
		}
	}
}

function Openlevel(level : String)
{
	Application.LoadLevel(level);
}

i have this collider going towards my person but when it hits my person nothing happens almost like its not recognizing it, i have to either move around a bit for the count to hit 1 or run over the collider again to make it hit 1. if i delete the count it still does the same thing. what am i doing wrong? :frowning:

Any gameobject that is moving and has a collider must also have a rigidbody component in order to report collision.

Also, make sure your collider is set to isTrigger

Without seeing your setup, it’s hard to be totally sure what the problem is. If you enter the trigger and it only sometimes seems to increase the counter, it’s likely that your player object has more than one collider on it, and that only one of your colliders has the tag “Player”. Then again, it can be something else entirely. Here’s how I’d get this to work though.

  1. Make sure your player object has a Rigidbody component, preferably on the top Transform of the prefab.

  2. Make sure the top-level GameObject of the prefab has your “Player” tag.

  3. Use the following code to detect whether the player has entered the trigger:

    function OnTriggerEnter(hit : Collider)
    {
    if(hit.attachedRigidbody != null)
    {
    if(hit.attachedRigidbody.CompareTag(“Player”))
    {
    count++; // and so on.
    }
    }
    }

By using the “attachedRigidbody” variable, you ensure that you’re dealing with the “parent” GameObject for the player and not any one particular collider that might be a child of the player GameObject. If you’re still having trouble, I’d suggest adding some Debug.Log statements inside the function to output the name of the hit collider, the collider’s tag, etc. It may be that you are assuming certain values are correct, but they’re actually different. Good luck!

thanks guys, it was just the adding of a rigid body on my collider. i didnt have to change the code or anything, the old one worked as soon as i added it. :slight_smile: