Script executing through walls

I’ve created a script that has the intent of, when the player sees the object, after ~1 second the object will disappear.
I’m having a problem that if the player looks in the direction of the object through walls, the script will execute and the object will be gone by the time the player turns to corner to see it.


void OnBecameVisible()

IEnumerator ExampleCoroutine()

    yield return new WaitForSeconds(2);


(Side note: I’m new to game development and scripting so sorry for any mistakes and or me not understanding a reply.)

Try using a raycast between the camera and the object to check if you can see it. It’s not perfect, because you are checking for a single point of the object, not its whole visible area, also, using colliders instead of renderers.
OnBecameVisible() calls are based on culling, not actual visibility of the object by the player