Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnBecameVisible not working

Discussion in 'iOS and tvOS' started by Crazy Robot, May 17, 2009.

  1. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    I placed a script in an object:

    Code (csharp):
    1. private var vis = 0;
    2.  
    3. function OnBecameVisible () {
    4.     renderer.enabled = true;
    5.     vis = 0;
    6.    
    7. }
    8.  
    9. function OnBecameInvisible ()
    10. {
    11.     renderer.enabled = false;
    12.     vis = 1;
    13.    
    14. }
    15.  
    16.  
    17. function OnGUI (){
    18.     if (vis == 1)
    19. GUILayout.Label ("InVis");
    20. }
    (I placed the GUI in to confirm the object is invisible because I can't put the editor camera on it)

    I made sure my editor camera is not on the object, when I start the game, the object is in view and is rendered and when the object goes out of view it becomes invisible as it should.

    BUT, when I turn around it does not become visible, it stays invisible.

    Can someone help me?

    Thanks,

    JL
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Once it's not visible, you turn the renderer off, so OnBecameVisible can never be called.

    Anyway, there's no point to this, since anything not in the view frustrum isn't rendered regardless.

    --Eric
     
    BAIZOR likes this.
  3. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    Yeah, I tried the same thing a while ago with the same results. It's never going to get the OnBecameVisible call because it's not being rendered. Catch-22.

    I think the best thing to use the OnBecameVisible/Invisible calls for is to turn off particle effects or animations while the object is off camera.

    It might be useful if Unity added an "OnBoundsInView" call so that we could do what you're trying to do.
     
  4. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Thanks,

    You guys have saved me a ton of "trying to get it to work".
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Animations are already disabled when off-camera, unless you choose "update when offscreen"; not sure about particles. I've only used this once, when I had some relatively heavy manually-computed mesh animation on some objects, which didn't need to be running if the player couldn't see them.

    What is it that you're trying to do? Any object off-camera isn't rendered anyway, as I mentioned.

    --Eric
     
  6. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    Sorry, I was composing when you posted. I guess I knew that because the drawcalls drops when stuff goes out of view. Probably isn't a need then.

    Edit: I just thought of something you could do with OnBoundsVisible. If you were doing a F.E.A.R.-style game where you wanted to spawn something scary, but not while the player is looking. The object could be placed with it's renderer off and not enable the renderer until the players back was turned (OnBoundsOutOfView). Then when they look back, YIKES!
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    One of the really great uses for it is changing UI states if you have onscreen information connected to them like position indicators, health bar etc. For example a position indicator might be especially of interest only while the object is offscreen.
     
  8. knik85

    knik85

    Joined:
    Dec 13, 2015
    Posts:
    3
    public class ViewTester : MonoBehaviour {

    MeshFilter meshFilter;
    public Mesh mesh;

    void Start()
    {
    meshFilter = GetComponent<MeshFilter> ();
    }

    void OnBecameVisible()
    {
    meshFilter.mesh = mesh;
    Debug.Log ("Became Visible");

    }


    void OnBecameInvisible()
    {
    meshFilter.mesh = null;
    Debug.Log ("Became Invisible");
    }
    }