Search code examples
c#unity-game-engine

How to make the onGUI method in Unity?


using UnityEngine;
using System.Collections;

public class GameRootScript : MonoBehaviour {

    public GameObject prefab = null;

    private AudioSource audio;
    public AudioClip jumpSound;

    public Texture2D icon = null;
    public static string mes_text = "test";


    // Use this for initialization
    void Start () {
        this.audio = this.gameObject.AddComponent<AudioSource> ();
        this.audio.clip = this.jumpSound;
        this.audio.loop = false;
    }

    void onGUI()
    {
        Debug.Log ("Image");
        GUI.DrawTexture (new Rect (Screen.width/2, 64, 64, 64), icon);
        GUI.Label (new Rect (Screen.width / 2, 128, 128, 32), mes_text);
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown (KeyCode.Z)){
            Debug.Log("Prefab");
            GameObject go = GameObject.Instantiate(this.prefab) as GameObject;
            go.transform.position = new Vector3(Random.Range(-2.0f,2.0f), 1.0f, 1.0f);

            this.audio.Play();
        }
    }
}

I make the onGUI() method in Unity, but the method doesn't work.
I just follow the book and I don't know what makes the problem.
Even I compile that code there is no error.
The Unity of book version is 4.xx, and my Unity version is 5.1.2.


Solution

  • You have a typo for your onGUI method. It should be OnGUI with a capitalized "O".

    With the typo in place, even though it compiles, the Unity engine essentially ignores this method.