Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnGUI not respecting call order?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Marc, Apr 10, 2008.

  1. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    I seem to have found something weird going on with the call order of OnGUI. It relates to the process loop. We know that Unity bascically calls functions in this order:

    Awake()
    Start()
    Update()
    LateUpdate()
    // Render

    I dunno if it is stated somewhere but i cant seem to find the way OnGUI() fits into this, so i made a test script.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CallOrderBug : MonoBehaviour {
    6.  
    7.     private Texture2D tex;
    8.    
    9.     void Awake() {
    10.         Debug.Log("CallOrderBug : Awake()");
    11.        
    12.         // Construct the texture.
    13.         tex = new Texture2D(100, 100, TextureFormat.ARGB32, false);
    14.        
    15.         for(int x = 0; x < tex.width; ++x) {
    16.            
    17.             for(int y = 0; y < tex.height; ++y) {
    18.                 tex.SetPixel(x, y, Color.red);
    19.             }
    20.            
    21.         }
    22.        
    23.         tex.Apply();
    24.        
    25.     }
    26.    
    27.     // Use this for initialization
    28.     void Start () {
    29.         Debug.Log("CallOrderBug : Start()");
    30.        
    31.     }
    32.    
    33.     void OnGUI() {
    34.         Debug.Log("CallOrderBug : OnGUI()");
    35.        
    36.         // Draw the texture
    37.         GUI.DrawTexture(new Rect(0, 0, tex.width, tex.height), tex);
    38.        
    39.     }
    40.    
    41. }
    42.  
    What i get from running this script is that OnGUI() is called before Awake() :eek: which basically means that i will get a null pointer exception as the texture is not initialized yet.

    Now this is prob. due to the fact that GUI runs in a seperate thread and not the process loop tread, is this correct?

    Now what this means is that i have to put in a S*** load of "if this is null" inside the GUI function which i could avoid if i could rely on my Awake() or start() to enable and disable the gui if something is missing, but i cannot atm. I would like to avoid it as OnGUI gets called alot.

    Is this a bug?

    Regards,
    Marc
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    That is a known bug, which will be fixed in the next release.

    How about having your script be disabled by default, then just setting enabled to true in Awake.
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Aye will do that thanks.