Search Unity

GUI wont display

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zayk, Nov 25, 2014.

  1. Zayk

    Zayk

    Joined:
    Oct 14, 2014
    Posts:
    6
    im making a two game; i want a GUI to pop up when the player enters a trigger i the player who has a collider and a rigidi body and the object which has a collider and is trigger on however no pop up when the player enters the trigger; this is my script please please help im a newbie in programming
    Code (CSharp):
    1. usingUnityEngine;
    2. usingSystem.Collections;
    3.  
    4. publicclassGuiPopUp : MonoBehaviour {
    5.  
    6. privatebooldisplayText = false;
    7.  
    8. voidOnTriggerEnter2d(Collider2Dcol){
    9. displayText = true;
    10. }
    11. voidOnTriggerExit2d(Collider2Dcol){
    12. displayText = false;
    13. }
    14. voidOnGui (){
    15.  
    16. if(displayText == true){
    17. GUI.Label (newRect(50, 50, 200, 50), "Dont go to the cave");
    18. }
    19. }
    20. }
    [/code]
     
  2. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    Your OnGui function is spelled wrong. It needs to be OnGUI. The U and I must be uppercase for it to work.

    Code (csharp):
    1.  
    2. [*]voidOnGUI (){
    3. [*]if(displayText == true){
    4. [*]GUI.Label (newRect(50, 50, 200, 50), "Dont go to the cave");
    5. [*]}
    6. [*]}
    7. [*]}
    8.  
    9.  
     
  3. Zayk

    Zayk

    Joined:
    Oct 14, 2014
    Posts:
    6
    I tried correcting that still nothing
     
  4. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    I just noticed that most of your functions are messed up. There should be spaces after void and public.
     
  5. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    Try this code. That might fix it.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class GuiPopUp : MonoBehaviour {
    5.  
    6. private bool displayText = false;
    7.  
    8. void OnTriggerEnter2d(Collider2Dcol){
    9. displayText = true;
    10. }
    11. void OnTriggerExit2d(Collider2Dcol){
    12. displayText = false;
    13. }
    14. void OnGui (){
    15. if(displayText == true){
    16. GUI.Label (newRect(50, 50, 200, 50), "Dont go to the cave");
    17. }
    18. }
    19. }
    20.  
     
    Last edited: Nov 25, 2014
  6. Zayk

    Zayk

    Joined:
    Oct 14, 2014
    Posts:
    6
    NickHaldon thanks man, still no pop up all im trying to do is when one player approaches another there should be a text box popping up with some word, probably there is another way to do it?
     
  7. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    No problem. Are you trying to do a 2D game? Unfortunately I don't have a lot of 2D experience so if it is I cant help much. On another note I set up my own scene and did manage to get it to work in 3D which shouldn't be hard to switch it to 2D if you want.

    To set it up make sure that your block or whatever you want them to hit to display the GUI is a trigger. If its not I don't think it will work. Here's the code I used to make it work. Its nearly identical to yours.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class GUIPopUp : MonoBehaviour {
    5.  public bool display1 = false;
    6.  
    7.  void OnTriggerEnter(Collider other) {
    8.  display1 = true;
    9.  }
    10.  void OnTriggerExit(Collider other){
    11.  display1 = false;
    12.  }
    13.  void OnGUI(){
    14.  if(display1){
    15.  GUILayout.Box("You triggered the collider");
    16.  }
    17.  }
    18. }
    19.  
    If you need anything else I'll try to help as much as I can.

    Good luck!
    NickHaldon