Whats happening with this?

So I am doing some iPhone Unity, and trying to get performance better, I am making a game object that contains a collider, and when collider is entered, it will enable coin scripts, and when its exited, it disables scripts. This is the code I’m using.

function OnTriggerEnter (colid : Collider) {
	
	if (colid.gameObject.tag == "Player") {
		var scripts = gameObject.GetComponentsInChildren (CoinScript);
		for (var tmp : CoinScript in scripts) {
		    tmp.enabled = true;
		}
	}
}

The problem is when my character enters, it will sometimes not turn on anything at all, or it will turn on one script, but it never turns on all. Any advice?

Try this (untested)

private var scripts : CoinScript[];

function Start() {
	scripts = gameObject.GetComponentsInChildren (CoinScript);
}

function OnTriggerEnter (colid : Collider) {
	if (colid.gameObject.tag == "Player") {
		for (var tmp : CoinScript in scripts) {
		    tmp.enabled = true;
		}
	}
}

function OnTriggerExit () {
		for (var tmp : CoinScript in scripts) {
		    tmp.enabled = false;
		}
}

I optimized a bit, but the important thing is that you needed an OnTriggerExit() function too.

Try running this with the inspector on debug and make sure that the scripts array is populated after start, if not that would explain why sometimes nothing turns on at all.

Alex.

Thanks for the help, I already had the trigger exit method, but forgot to post it. I figured out what the problem was though, it seems that I (stupidly) made it so in my coin script its reference to “transform” was a static var. Making it so each script was accessing the same coin. I fixed this and now everything works. Thanks!