- Home /
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.
Answer by syclamoth · Oct 04, 2011 at 09:54 AM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612234529im_/https://answers.unity.com/themes/thub/images/avi.jpg)