disable a script when a object is hidden?

hello how do you disable a script on a hidden object and then enabled when its un hidden here is my hiding script.

function Update () {
renderer.enabled = false;
var enemyCount : int = GameObject.FindGameObjectsWithTag("Block A").length;
if(enemyCount <= 0)
{
renderer.enabled = true;
}
}

but when my object is hidden this code is still working(below) when i walk over the hidden object, i can tell because a particle effect pops up.:

var objectToMake : Transform;
var spawnPos : Transfrom;
var stars : ParticleEmitter;
function OntriggerEnter (col : collider)
{
Destroy(gameObject);
Instantiate (objectToMake, spawnPos.Position, spawnPos.rotation);
Instantiate (stars, transform.postion, transform.rotation);
}

so basically how do i disable it when the prefab/object is hidden?

Can you fix your formatting, please? It's kind of hard to read.

1 Answer

1

Disabling everything when you hide it is easy- you just have to use

gameObject.enabled = false;

however, this disables everything about the object, so it can no longer turn itself back on again! You would have to re-enable it from outside.

Instead, you can put a qualifier inside of your OnTriggerEnter function, like this-

if(renderer.enabled)
{
   // do your stuffs 
}

This way, it makes sure that the renderer is enabled (i.e, the object is visible) before executing your trigger code!