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

Question How to check if game object is actually currently displayed

Discussion in 'Scripting' started by MaxLohMusic, Jun 20, 2023.

  1. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    45
    obj.gameObject.activeSelf will return true even before the frame was updated to actually display it.

    How do we find out whether it's actually currently displayed during that frame (without needing to use unnecessary complications such as camera)?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    but but...

    ... then what camera is it currently displayed to?

    if you don't want to hear about cameras then the answer to "is it actually displayed?" is always
    false


    Or perhaps I am misunderstanding your question?
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    SeerSucker69 likes this.
  4. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    45
    I might be wrong based on how unity does things, but in my mind a camera shouldn't be needed to do what I was asking. Unity should know somewhere in the guts whether an object that was recently set to "active" is actually active in this frame. That's a totally different problem from seeing whether something is within a camera's view and needing to take into account where you're looking, the screen projection etc.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    These are completely separate things.

    "Being active" means being considered in the internal loop or skipped over. If the whole object is set to inactive, all its component are thus inactive as well, including its MeshRenderer.

    "Being displayed" means getting actively rendered by a camera. You need a camera for that, there is no "displayed" flag. The object's mesh is either considered by the renderer or not, either through 'active state' flag or through layer masks, and the camera is what actually drives this behavior.

    That said, you can turn off the actual MeshRenderer component, but without some more context you're bound to confuse yourself further.
     
    Kurt-Dekker likes this.
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Unity waits for the next frame before having several modules iterate through all objects and deciding on what to do with them. Activating an object in the current frame doesn't have any impact on anything in the current frame. Although another script could in theory test to see if an object has just been activated before the next frame. I guess this is where you're having problems.

    Perhaps you could look into the execution order of your scripts so that the scripts that are polling objects for becoming active are executed before the scripts that do the activating. This then guarantees that the polling scripts are always a frame behind.
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    So to answer your question, a) you check whether MeshRenderer is active (and the object is eligible for rendering, i.e. camera's layer mask matches the object's layer etc.), then b) you check whether the object's bounding box overlaps the camera frustum (see this example).

    If both tests pass, the object is currently displayed during that frame. If you change something, it will probably will become the truth for the next frame. And even if I'm wrong about the exact timings, I'm certain that this behavior is consistent, allowing for further logic to be built around it.

    A complication such as camera is what actually provides the display. If you turn off the camera you can rest assured that nothing will be displayed whatsoever.

    You can also try playing with these things, although you need to experiment a little
    Renderer.isVisible
    Renderer.OnBecameVisible
    Renderer.OnBecameInvisible
     
    Kurt-Dekker likes this.