A bit of a Triggering problem....

I am trying to count how many times a trigger is triggered. In my script I added a variable and an increment. But when it collides once my counter hits one it stops. I don't know why.

Here is my code:

function OnTriggerEnter( hit : Collider )


if(hit.gameObject.tag == "Test")


var counter = 0;

counter ++;

var a = GameObject.Find("NewText");

var textMesh : TextMesh = a.GetComponent(TextMesh);

textMesh.text = counter.ToString();



Am I supposed to add something in the auto-generated Update Function?

the problem is that you're declaring counter inside the function - every time you collide, it'll make a new variable and set it to 0

what you need to do is put var counter = 0; outside of the function, then it should work fine