Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

OnGUI not being called

Discussion in 'Extensions & OnGUI' started by endanegered, May 23, 2018.

  1. endanegered

    endanegered

    Joined:
    Aug 18, 2017
    Posts:
    4
    Hello, I'm new and have just been learning Unity the past couple weeks.

    I have been trying to make a simple card game and made GUI buttons to debug/verify new functions. It was working previously and then I'm not sure where it got messed up and I can't get it to work anymore. It stopped working in when I added a draggable script and added the event system and removing those two elements does not have any effect.

    This script is attached to a GameObject in the hierarchy and I have messed around with the layers, coordinates, and have not gotten anything to work. I have also tried adding it to the canvas, event system, camera, and attaching the DeckModel object to it before hitting play. The "DebugDeck" does not show up on the console so I know its not a matter of finding the buttons in the space. I'm sure its probably something small I'm missing or possibly clicked on. Let me know if I need to add or clarify anything.

    Any help is appreciated! Thanks!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DebugDeck : MonoBehaviour {
    6.     [SerializeField] private DeckModel deck;
    7.     bool hit = false;
    8.  
    9.     public void OnGUI()
    10.     {
    11.         float xPos = Screen.width / 2;
    12.         float yPos = Screen.height / 2;
    13.         Debug.Log("DebugDeck");
    14.  
    15.         if (GUI.Button(new Rect(xPos, yPos, 100, 28), "Press"))
    16.         {
    17.             if (!hit)
    18.             {
    19.                 deck.DisplayCards();
    20.                 deck.ShowFaces();
    21.                 hit = true;
    22.             }
    23.             else
    24.             {
    25.                 deck.HideFaces();
    26.                 hit = false;
    27.             }
    28.         }
    29.  
    30.         if (GUI.Button(new Rect(xPos, yPos + 1, 100, 28), "Shuffle"))
    31.         {
    32.             deck.Shuffle();
    33.             deck.RemoveDisplay();
    34.             deck.DisplayCards();
    35.         }
    36.  
    37.         if (GUI.Button(new Rect(xPos, yPos + 2, 100, 28), "Reorder"))
    38.         {
    39.             deck.RemoveDisplay();
    40.             deck.InOrder();
    41.             deck.DisplayCards();
    42.             deck.ShowFaces();
    43.         }
    44.  
    45.         if (GUI.Button(new Rect(xPos, yPos + 3, 100, 28), "Deal"))
    46.         {
    47.             deck.Deal();
    48.         }
    49.  
    50.     }
    51. }
     
    Last edited: May 24, 2018
  2. endanegered

    endanegered

    Joined:
    Aug 18, 2017
    Posts:
    4
    I just ended up just creating a UI and putting buttons on there... Still couldn't figure out what happened with the script.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    81
    The only thing I can think of is that the script wasn't enabled.