- Home /
On can I have OnBecameVisible/Invisible ignore the scene/editor camera and only work for the game camera ?
everything is in the title.. I just want to avoid checking if my object is visible in my update function (so every frame) and would like to use unity's OnBecame * functions for that But unfortunately it doesn't work from the editor
any idea ? thanks in advance !
Can you rephrase the question please? I don't understand it... Sorry.
He seems to be saying that he wants some way to tell when his objects leave the view of his main camera, but not in-game; Through the editor.
Oh... seems a little pointless, but thanks for the clarification lol.
yeah looks like I've typed a bit too fast.. basically I want to use OnBecameInvisible/Visible (in order to automatically kill/respawn my gameobject, so I can't really use OnWillRenderObject ins$$anonymous$$d..) the issue is that the scene/editor camera is also used for that (not only the game camera) I was wondering if there's a way to fix that it's really annoying actually because the game behaves differently when played in the editor than when played as a standalone I'm sure this would beneficial to a lot of people if this was fixed on the unity side (what about a project option somewhere?) cheers
Answer by nomadic · Dec 03, 2014 at 04:35 PM
Here's what I've been doing:
void OnBecameVisible()
{
#if UNITY_EDITOR
if (Camera.current.name == "SceneCamera")
return;
#endif
// put your code here
}
It dose not work. If one gameObject is visible by gameCamera both in Scene and Game, OnBecameVisible() will be called twice.
it works for me
(not perfectly, since the events don't always get called from the game camera if the scene view is visible), but it works if you are displaying only the game view
Thanks
Answer by Bunny83 · Mar 29, 2011 at 03:29 AM
I had some similar issues with AnimateOnlyIfVisible
. You can't watch your object not being animated because at the moment any camera sees it, it will animate. Unfortunately as far as i know there's no way to go around that.
The only thing you can try is not using OnBecameVisible
but Camera.OnWillRenderObject. OnWillRenderObject
is a message that is sent by the camera to your object. I'm not sure if this is called camera independently, but as far as i can tell you should be able to sort out which camera is rendering the object. Just check Camera.current if that's your desired camera or not.
Answer by el_santo · Apr 08, 2015 at 08:48 AM
I see only one way:
if UNITY_EDITOR
public void FixedUpdate()
{
if (MyFunctionIsObjectVisible(transform.position))
FireOnBecameVisible();
}
else
protected void OnBecameVisible()
{
FireOnBecameVisible()
}
endif
Answer by TimNick151297 · Feb 27, 2016 at 03:58 PM
A different solution is just to specify the camera with a tag. For example:
function Update () {
if (Camera.current.tag == "MainCamera")
{
if (this.GetComponent.<Renderer>().isVisible == false)
{
//do something
}
}
}
Note that this only works in fullscreen playmode, not minimized view.
Answer by Khaled_A_Younes · Dec 28, 2016 at 11:59 AM
This is answer is probably long overdue, but the solution that worked for me was to close the scene view tab.
Unity docs has this to say about OnBecameInvisible:
Note that object is considered visible when it needs to be rendered in the scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the scene view cameras will also cause this function to be called.
Your answer
Follow this Question
Related Questions
Editor Camera Orthograpric Control 5 Answers
Problem accessing the scene camera 1 Answer
Modify SceneCamera's position in script? 2 Answers
Drawing Handles 1 Answer