- Home /
If gameObject is invisible - disable it... what's wrong with this code?
Hi everyone,
I am in the process of optimising my game. I have a script that turns off all of the onscreen objects' behaviour if they are not visible in the renderer and it works great, but when I tried to add this script to actually disable any off screen objects, it just disables them all whether they are onscreen or not... what am I missing?
function Update ()
{
if (gameObject.renderer.isVisible)
{
gameObject.active = true;
}
else
{
gameObject.active = false;
}
}
Answer by Seth-Bergman · Jul 20, 2012 at 11:16 PM
it's probably this:
http://answers.unity3d.com/questions/41962/renderisvisible-returning-false-for-one-frame.html
Ah, doh... if it becomes invisible and is disabled... of course it's not going to be visible again... H$$anonymous$$$$anonymous$$$$anonymous$$...
So you are right, but this begs a new question... which I shall now ask ;-)
good point! not active means not visible, so once it's disabled it's disabled for good. If the object hasn't finished rendering in the very first frame, when the code is first called, it gets turned off, for good
And therein likes the crux of the problem. LOL. AAAAARGH!!!