C# OnBecameInvisible Weird behaviour

Hi Guys,

I’ve got issues using OnBecameInvisible().
I’ve been searching on the internet and i found the classic issue “Make sure sur Editor Camera isn’t looking the objects”.

My issue is different:

  • I’m Instantiating prefab, 2 of them
  • Each prefab has a simple script with OnBecameInvisible() {Debug.Log(transform.name);}
  • Not other scripting involved

The very weird behaviour is the following :

  • If both prefabs are visible at the same time, the first pefab to be invisible will wait for the other to be invisible before calling is own OnBecameInvisible()…

  • If 1 prefab is visible and the other invisible from the start(). The first one will just do fine and Debug.Log its name when OnBecamInvisible().

I have spent 2 hours trying to understand… The code is super easy, so i’m wondering what could be the issue… Something to do with prefabs ?..

Here is how i generate both prefabs, and the camera moves (attached to the only camera on the scene)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SelectPlateform : MonoBehaviour {

    public List<GameObject> myPlateformLists;
    public int maxPlatform;
    public GameObject myPlatformPrefab;

    // Use this for initialization
    void Awake () {
  
        for(int i = 0; i<= maxPlatform; i++){
            GameObject myPlatform = Instantiate(myPlatformPrefab) as GameObject;
            myPlatform.transform.position = new Vector3( + i * myPlatform.transform.localScale.x *30,0,0);
     

        }

    }
  
    // Update is called once per frame
    void FixedUpdate () {

        Camera.main.transform.Translate (new Vector3(4,0,0) * Time.deltaTime);
  
    }
}

Here is the OnBecameInvisible script attached to the prefabs :

using UnityEngine;
using System.Collections;

public class PlatformScript : MonoBehaviour {

    void Update(){


    }

    void OnBecameInvisible(){

        if(transform.position.x <= Camera.main.transform.position.x){
            Debug.Log(transform.name);
        }
    }

}

Could be dynamic batching messing things up?
Try toggling the Dynamic Batching option and see if the result’s the same.

You can find the option here: File > Build Settings > Player Settings… > scroll down to Other Settings column > Dynamic Batching

Be warned though, disabling dynamic batching may affect your game performance in other areas.

Note that OnBecameVisible and OnBecameInvisible are also associated with the editor window.

Another guess:
As you mentioned that it only triggers if both are out of sight, it might be that your platform prefab ‘lives’ in the scene. Make sure your prefabs exist in the project folder and you dragged it from there (!!), they serve as a template kinda like the definition of a class, whereas scene objects can be thought of as instances.
So unless you’re really sure you need a scene object as ‘prefab’, always use prefabs from the project folder in order to avoid unexpected behaviour.

Hey,

Thanks for the advice.

I started disabling the Directional Light and it worked fine… As it didn’t make sense i though it could be one of the image effects on my camera… Flares or whatever… And i finnaly found out that the problem came from the Light itself, and the realtime baking.

Can someone explain me of Realtime baking works and why the hell it affects OnBecameInvisible ? :smile:

Thanks !

edit : thanks pixpusher2, i just saw your message ! So the problem was the realtime bake on the light… I don’t really get why, but the issue is solved… :stuck_out_tongue: