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

Using OnGUI

Discussion in 'Scripting' started by MohammadM, Jun 10, 2014.

  1. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    Hello.
    I am trying to create a Dialog system.
    I have more than one scenes which i will be using this dialog system with. i don't want to keep writing the OnGUI function for each scene. So i thought i will create a class called DialogManager. This class will contain the OnGUI function responsible for displaying the GUI. and then for each scene i would create an object of it. and just changing the text of dialog as required. This is how the classes look like.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. Dialog Manager
    4. {
    5.  
    6.     int DialogPX;
    7.     int DialogPy;
    8.     bool hD;
    9.     bool hB;
    10.     public    string textDialog;
    11.     public  string textButton;
    12.     //----------------------------------------
    13.     void Start () {
    14.         DialogPX = Screen.width / 5;
    15.         DialogPy = Screen.height / 2;
    16.         hD = false;
    17.         hB = false;
    18.     }
    19.  
    20.     //----------------------------------------
    21.     void OnGUI () {
    22.         GUILayout.BeginArea (new Rect(DialogPX,DialogPy,400,400));
    23.         if(HideDialog==false)
    24.         {
    25.             GUILayout.Label(WrightDialog(textDialog));
    26.         }
    27.         GUILayout.EndArea ();
    28.     }
    29.     //----------------------------------------
    30. public string WrightDialog(string text)
    31.     {
    32.         return this.textDialog = text;
    33.     }
    34.   public  bool HideDialog(bool hD)
    35.     {
    36.         return this.hD = hD;
    37.     }
    38.   public  string AddButtons(string text)
    39.     {
    40.     return    this.textButton = text;
    41.     }
    42.    public bool HideButtons(bool hB)
    43.     {
    44.         return this.hB = hB;
    45.     }
    46.     //----------------------------------------
    47. }
    48.  
    And this is the scene class which i attached to a game object
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scene0 : MonoBehaviour {
    5.  
    6.     public DialogManager Dialog;
    7.     void Start () {
    8.        Dialog = new DialogManager ();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         Dialog.WrightDialog("Hello This Not working !!");
    14.  
    15.     }
    16. }
    17.  
    Now this is not working. I think its because the OnGUI function is never being called. I have tried to make the OnGUI function public and call it from the scene script but unity would give me a warning saying u cant call a GUI function if ur not inside OnGUI. I also changed the Update Function to OnGUI and called the Dialog.OnGUI, The text will display but it did not consider the GUI.BeginArea Function.

    Any solution for this?
    Thanks.
     
    Last edited: Jun 10, 2014
  2. mhtraylor

    mhtraylor

    Joined:
    Jan 13, 2013
    Posts:
    33
    @MohammadM, your DialogManager class is not a MonoBehaviour, so none of the Unity initialization and update callbacks are being called (such as Start() and OnGUI()).

    So, when you called Dialog's OnGUI method in scene0's OnGUI method, the GUI code was called but the fields DialogPX and DialogPY were not set to the values you wished by the Start method, and thus your GUILayout area was set to a Rect with whatever C# sets uninitialized value types to be... a zero value, I believe. Which might explain the problem with the GUI area.
     
    MohammadM and Graham-Dunnett like this.
  3. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    WaaW have no idea how did I miss that. Thank you v much m8.
    I changed the scene Script to look like this.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scene0 : MonoBehaviour {
    5.  
    6.     public DialogManager Dialog;
    7.     //----------------------------------------
    8.     void Start () {
    9.         Dialog = new DialogManager ();
    10.    
    11.     }
    12.    
    13.     //----------------------------------------
    14.     public    void OnGUI () {
    15.         Dialog.WriteDialog ("Hello");
    16.         Dialog.Start ();
    17.         Dialog.OnGUI ();
    18.  
    19.     }
    20.     //----------------------------------------
    21.  
    22. }
    So as u said since I am not using MonoBehaviour ,the functions are not being called. so i called them my self ...Cherrz :)