In my scene I instantiate a prefab in front of the camera. Mean while the camera is slowly moving to the right along the X axis. So after a small amount of time the prefab is off screen.
When that happens I want to delete/clean up that prefab, since it’s not being used anymore. This is how my script that is attached to a prefab looks like:
public class Floor : MonoBehaviour {
public GameObject floor;
void Start () {
Vector3 pos = new Vector3(200f,60f,10f);
Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(pos);
Logger.AddLog("cam", "campos: " + realWorldPos.ToString());
// Put floor in the scene
GameObject firstFloor = (GameObject)Instantiate(floor);
firstFloor.transform.position = realWorldPos;
}
void OnBecameInvisible() {
Logger.AddLog("bla", "WOOT");
}
void Update () {
Logger.AddLog("vis", "visible");
}
}
The “OnBecameInvisible” method never seems to get called. I also tried to use “renderer.isVisible”. But that gives me the error that “renderer” is not set to an instance of an object.
So what am I doing wrong here?