Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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:
    450
    The only thing I can think of is that the script wasn't enabled.