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

Resolved renderer.isVisible and OnBecameInvisible are not agreeing. Huh?

Discussion in 'Scripting' started by UnbridledGames, Feb 23, 2021.

  1. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    First, I'm aware of the scene view camera issue. That's not the problem.

    I'm trying to sort a list of objects based on how close they are to the camera, with any that are not visible being sent to the back of the line. However, it's been only giving me the closest object, regardless of if it's on screen.

    These objects have made use of OnBecameVisible and OnBecameInvisible for a while for some internal script stuff unrelated to this function.

    A different script handles this sorting, and using renderer.isVisible was easier in this case. Well, everything is always visible when checking isVisible. (Ok, not always, there are ways I can turn the camera that seems to make them go invisible, but thats not the real issue.)

    The issue is I put code inside the OnBecameInvisible functions to log the isVisible status of the renderer attached to the object.

    When these objects go off screen and OnBecameInvisible triggers, it's still saying isVisible is true.

    I then added a coroutine to print the isVisible status for a few seconds after. Even though OnBecameInvisible is called and works correctly (and OnBecameVisible does as well), isVisible is still saying yes, it's visible.

    Whats going on? Any ideas?
     
  2. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hi,
    I think your situation can be explained by the documentation about renderer.isVisible:
    https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html

    "Note that the object is considered visible when it needs to be rendered in the Scene. For example, it might not actually be visible by any camera but still need to be rendered for shadows. When running in the editor, the Scene view cameras will also cause this value to be true."

    Could that be it?
     
    UnbridledGames and Kurt-Dekker like this.
  3. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    I don't believe so, this is for a simple AR scene and the shaders don't use shadows. Though, I will admit ignorance in that this is a mostly-UI based app with some AR thrown in, so complicated light and shadow is not anything I've paid attention to or even worked with yet.

    Where would shadows be determined necessary on these objects? On the object material themselves, scene lighting/settings, somewhere else?
     
  4. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    Aah crap, the objects in the Mesh Renderer settings are flagged as casting and receiving shadows. I'll try turning those off and see what happens.

    Edit: That was it. Making sure they did not cast shadows made isVisible line up with OnBecameInvisible. Thanks for the tip about the shadows. I read originally but it didn't sink in the first time around.
     
  5. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Great to hear! :)
    Honestly I didn't know about that shadow issue either, I was just glad to find that note on the doc page that seemed to fit your case :D
    Don't forget to mark the thread as Resolved by going to Thread Tools (top of the page) -> Edit Title -> add the prefix "Resolved".
    Thanks!
     
    UnbridledGames likes this.
  6. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    Thanks again, never thought about adding resolved to the title. Be nice if that were a simple forum feature too. Just click a button and it's tagged as resolved. I'll try and go back and do that with my other questions as well.