Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved OnBecameVisible / OnBecameInvisible run in editor despite no cameras being on it

Discussion in 'Scripting' started by cyangamer, Feb 18, 2021.

  1. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    235
    Yes, I know about the Scene camera quirk.

    There seems to be some sort of issue in Unity 2018.4.31 that I just discovered. I have my GameObjects set up so that their script components do not activate until they are visible to a camera.

    When I start up my scene, there is a quick blip where they are visible and then not visible again. I took care in facing the Scene camera away from everything. I even closed the window entirely, and disabled every camera. However, a handful of game objects are still having this blip. Here's the code I'm using:

    Code (CSharp):
    1. public class EnemyActivatorForRenderer : MonoBehaviour
    2.     {
    3.  
    4.         public void OnBecameVisible()
    5.         {
    6.             EnemyActivator ea = gameObject.GetComponentInParent<EnemyActivator>();
    7.             if (ea != null)
    8.             {
    9.                 Debug.Log(transform.parent.gameObject.name+" <color=blue>is visible!</color>");
    10.                 ea.OnBecameVisible();
    11.             }
    12.         }
    13.  
    14.         public void OnBecameInvisible()
    15.         {
    16.             EnemyActivator ea = gameObject.GetComponentInParent<EnemyActivator>();
    17.             if (ea != null)
    18.             {
    19.                 Debug.Log(transform.parent.gameObject.name+" <color=blue>is NOT visible</color>");
    20.                 ea.OnBecameInvisible();
    21.             }
    22.         }
    23.     }
    I'll see if I can create a bug report, but does anyone know a workaround? What I need is for my scripts to not run until the player can actually see them.
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    783
    cyangamer likes this.
  3. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,290
    You could try to adding Debug.Log(Camera.current), to check if any camera is in fact rendering. Camera.current should be the camera in context for those function. Reflection probes also use a camera, so it's possible they are triggering the functions.
     
    cyangamer likes this.
  4. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    235
    The renderer is on the gameObject, which calls a script also called OnBecameVisible() that I implemented on the parent object. This is because the visual model is (in most cases) a child of my gameObject which just has the behaviors, colliders, and what have you.

    This is it. I added that and got this back:

    Code (CSharp):
    1. Mask Camera (UnityEngine.Camera)
    2. UnityEngine.Debug:Log(Object)
    3. VisionBreak.Sunsear.EnemyCore.EnemyActivatorForRenderer:OnBecameVisible() (at Assets/Sunsear/Scripts/EnemyActivatorForRenderer.cs:15)
    I have no idea what a "Mask Camera" is, but I guess Unity's functionality is vindicated here. There shouldn't be any reflection probes being used since this is a mobile game, but there's clearly something unexpected going on.

    Thanks for your responses! Any idea what could be creating that phantom camera object?
     
    Last edited: Feb 19, 2021
  5. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,290
    Mask Camera could be from a third-party asset, using a hidden camera. The name is pretty specific, so you could do a search for it in your code base.

    In your OnBecameVisible function you can do a check on the camera type, to see if it's an actual "Game" camera, if not, bail out

    Code (CSharp):
    1. //Skip for any special use camera's (except scene view camera)
    2. if (camera.cameraType != CameraType.SceneView && (camera.cameraType == CameraType.Reflection ||
    3.                                                   camera.cameraType == CameraType.Preview ||
    4.                                                   camera.hideFlags != HideFlags.None)) return;
     
    cyangamer likes this.
  6. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    235
    What I decided to do was have a boolean method check for this Mask Camera. I found the source of it (it's some post-processing stuff), and having the OnBecameVisible functions bail on all non-game cameras was giving it problems. I now know how to find the other hidden cameras so if they start causing problems I can filter them one at a time.

    Code (CSharp):
    1.  
    2. public bool IsInvalidCamera(Camera camera) {
    3.             if (camera == null) return true;
    4.             return camera.cameraType != CameraType.SceneView
    5.                 && (camera.name == INVALID_MASK_CAMERA);
    6.         }
    7.  
    Thanks so much for your help! :D