Make object disappear when out of sight

Hello!

I’m attempting my first Unity game as a final project for a computer science course I’m taking. I do have some very limited experience in Javascript, and a bit of experience in other game engines, but beyond that I have no idea what I’m doing.

Now, this may not be possible, but I was wondering: is it possible to make an object disappear/be destroyed when it’s not in sight any more? I have NO idea how I would accomplish this, just think it’d be a cool feature. Right now in my game you shoot cats out of a pet cage (instead of bullets out of a gun), and I wanted to make it so that the cats disappear from the ground when I look away from them.

Thank you for your time, and I hope this hasn’t been too confusing or too much of a hassle.

-Artoria

So, let’s say you attach a script to the cat that checks its renderer. There’s a property you can check, isVisible, or you can use the event functions OnBecameVisible and OnBecameInvisible.

Bear in mind that Unity’s idea of “visible” might not strictly agree with yours. If any camera can see the object, I believe including the scene camera, it’s visible. If the axis-aligned bounding box of the object enters any camera’s view area, it’s visible. If it’s casting shadows in the scene that are visible? Still visible! Unity’s a little pernicious about it.

So, anyway, let’s suppose you don’t mind any of that. A rough idea of visibility is fine. You could attach a script something like this:

function OnBecameInvisible() {
	Destroy(gameObject);
}