Search Unity

OnBecameInvisible PROBLEM

Discussion in 'Scripting' started by Simonxzx, Apr 23, 2015.

  1. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi everyone, i'm trying to destroy some objects I instantiate when they're out of the camera view. I've written a script (that I put below) and attached it to the instantiated objects that will be destroyed, but it doesn't work. Any idea ? Thank you.


    Code (CSharp):
    1. void OnBecameInvisible() {
    2.          Destroy(gameObject);
    3. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Just played around with this and have drawn a few conclusions:

    - The script has to be on the same GameObject as the Renderer component or it will not be called. Putting it on a parent will not work.

    - It looks like in the editor that both the game camera and the scene camera have to be facing away from the object. It's not called if either camera has the object in view.

    Hope this helps you debug.
     
    hamsterbytedev likes this.
  3. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    I managed to solve the problem in another way, but thank you very much guy ! :)
     
  4. vlab22

    vlab22

    Joined:
    Nov 22, 2012
    Posts:
    17
    Check if you have another Camera seeing that gameobject(MeshRenderer).
    Even if this "hidden" camera is in a lower depth.