- Home /
Render.IsVisible returning false for one frame
I have a gameobject that starts out in the center of the camera at startup. For some reason the first frame when the game starts the Gameobject.renderer.IsVisible returns false for just one frame then returns true for the remaining frames. I have this object coded to move down the screen and have code in place to destroy it when it goes offscreen
void Update()
{
// If gameobject is off screen
if (!gameObject.renderer.isVisible)
{
Destroy(this.gameObject); // Destroy the gameobject
}
}
Now since the GO is considered not visible for the first frame it is destroyed immediatelly. Can anyone tell me why its doing this?
Answer by jakerman999 · Jan 09, 2011 at 04:08 PM
It could be that your update(); function is being executed before the falling object is done rending, and thus the isVisible property hasn't been set to true yet. Try putting in a boolean that determines if the object was ever visible, and only run the function after that boolean was set.
Thanks for the answer jakerman, i was thinking of doing this and using the onbecamevisible to set the bool to true. But i also have another script doing the exact same thing but for a projectile that gets instantiated and it works perfectly. I was wanting to make this work without adding that but it looks like i may have to =P thanks again.
Answer by ayyappa · Jan 02, 2015 at 12:01 PM
You can use a coroutine in the following manner instead of going for another variable.
public class NotifyIfRendererInvisible : MonoBehaviour {
public delegate void OnRendererInvisible();
public OnRendererInvisible OnRendererInvisibleCallback;
void Start()
{
StartCoroutine(CheckForVisibility());
}
//This is a coroutine to check after the end of frame if its visible or not.
IEnumerator CheckForVisibility()
{
while(true)
{
yield return new WaitForEndOfFrame();
if (OnRendererInvisibleCallback != null && !renderer.isVisible)
{
OnRendererInvisibleCallback();
}
}
}
}
I made it event based, if you want you can remove this and destroy the object directly. But I suggest to pool instead of destroying the objects runtime.