- Home /
Destroy Game Object When Visible to Player
I have a game object that I want to be destroyed after a slight delay, but only when the player has an unobstructed view of it. Essentially, the moment it becomes visible on screen, rather than when the player is looking directly at it. I've searched for a while, and can't seem to find any answers to this. I was wondering if anyone could offer suggestions. Thank you.
Answer by WinterboltGames · Sep 23, 2017 at 03:40 PM
Use OnBecameVisible
public void OnBecameVisible () { Destroy(gameObject); }
That was the first thing I tried, but I couldn't get it to work for some reason.
At this point, all I have is
void OnBecameVisible (){
Destroy (gameObject);
}
But, as far as I can tell, that causes the objects to be destroyed immediately whenever they enter the main camera's field of view, whether or not they're obstructed from view.
$$anonymous$$ake sure you have a mesh renderer attached to that gameObject with the OnBecameVisible $$anonymous$$ethod.