I am drawing a screenspace box round objects using GUI.box. It works but there’s a strange effect I don’t understand: for every LateUpdate() I seem to be getting two OnGUI().
Here’s my code snippet:
Rect box;
bool boxing = false;
// other code to manage state of boxing
//GUIRectWithObject() returns a screen space Rect
void LateUpdate ()
{
Renderer rend = this.gameObject.GetComponent<Renderer> ();
if (boxing && rend.isVisible) {
box = GUIRectWithObject (this.gameObject);
Debug.Log ("Defined: " + box);
}
}
void OnGUI (){
Renderer rend = this.gameObject.GetComponent<Renderer> ();
if (boxing && rend.isVisible) {
GUI.Box (box, GUIContent.none);
Debug.Log ("Drawn: " + box);
}
}
The log looks like this:
Defined: (x:41.08, y:225.82, width:25.11, height:18.45)
UnityEngine.Debug:Log(Object)
EnclosedObject:LateUpdate() (at Assets/Scripts/Output scripts/EnclosedObject.cs:44)
Drawn: (x:41.08, y:225.82, width:25.11, height:18.45)
UnityEngine.Debug:Log(Object)
EnclosedObject:OnGUI() (at Assets/Scripts/Output scripts/EnclosedObject.cs:52)
Drawn: (x:41.08, y:225.82, width:25.11, height:18.45)
UnityEngine.Debug:Log(Object)
EnclosedObject:OnGUI() (at Assets/Scripts/Output scripts/EnclosedObject.cs:52)
This repeats with two drawn messages for each defined message. Can anyone tell me what is going on? Thanks.