Search Unity

You can only call GUI functions from inside OnGUI but I'm inside OnGUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by LockManipulator, Feb 19, 2017.

  1. LockManipulator

    LockManipulator

    Joined:
    Feb 14, 2017
    Posts:
    4
    This is my code: http://pastebin.com/ZegEbNWC I don't know how to format code on here so I used pastebin. I made a game object and attatched this script. Then I have an input field and under On End Edit I added the game object. Using UIController.SetPlayerName works. When I type something in and hit enter then inputText variable equals what was typed. Then I added clicked "+" under On End Edit and added the game object again and tried to use UIController.OnGUI to show the text that the player entered but I get the error "You can only call GUI functions from inside OnGUI" but I have a function called OnGui and it's inside that function that I'm trying to show the text entered by the player. What's going wrong and how can I fix this? I'm just trying to have the player enter something, then I show what the player entered on the screen.

    I feel like I'm missing something very obvious.
     
    Last edited: Feb 19, 2017
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You shouldn't call the OnGUI function manually, Unity will call it for you every frame.
     
  3. LockManipulator

    LockManipulator

    Joined:
    Feb 14, 2017
    Posts:
    4
    What part is calling it manually? You mean I just take out "public void OnGUI()"?
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It sounds like you are calling UIController.OnGUI when the input field is finished editing.
     
  5. LockManipulator

    LockManipulator

    Joined:
    Feb 14, 2017
    Posts:
    4
    Ah I see what you mean now. With that part out, it isn't giving errors but it also isn't doing anything. Is there something I should do to set text size and color to be visible? Or maybe something to bring it to the front and not be behind other layers? It'll be over a black panel. I changed the color to white but it still doesn't show up.