Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI problem !

Discussion in 'Scripting' started by Louis-LCM, Nov 28, 2014.

  1. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Hi guys ... I have a problem ... I want when I pass a level ... a GUI Pop up appear, show a "Level Up!" message and dissapear after 3 seconds ... any1 can help me ?

    Here is my script :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelPopUp : MonoBehaviour {
    5.    
    6.     private int coins = 0;
    7.     public GUISkin coinsSkin;
    8.     public GUISkin levelSkin;
    9.  
    10.    
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.         coins = PlayerPrefs.GetInt("Coins", 0);
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.        
    22.     }
    23.    
    24.     void FixedUpdate () {
    25.         PlayerPrefs.SetInt("Coins",coins);
    26.     }
    27.    
    28.     void OnGUI()
    29.     {
    30.         DisplayLevel ();
    31.     }
    32.    
    33.     void DisplayLevel()
    34.     {
    35.         GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,new Vector3(Screen.width / 480.0f, Screen.height / 320.0f, 1));
    36.         GUI.skin = levelSkin;
    37.         Rect coinIconRect = new Rect(200, 150, 32, 32);
    38.        
    39.         //LEVEL SHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    40.         string skillLevel = " ";
    41.        
    42.         if(coins >= 7500){
    43.             skillLevel += "Level Up!";
    44.         }
    45.         else if(coins >= 6900){
    46.             skillLevel += "Level Up!";
    47.         }
    48.         else if(coins >= 5600){
    49.             skillLevel += "Level Up!";
    50.         }
    51.         else if(coins >= 4500){
    52.             skillLevel += "Level Up!";
    53.         }
    54.         else if(coins >= 2300){
    55.             skillLevel += "Level Up!";
    56.         }
    57.         else if(coins >= 1600){
    58.             skillLevel += "Level Up!";
    59.         }
    60.         else if(coins >= 800){
    61.             skillLevel += "Level Up!";
    62.         }
    63.         else if(coins >= 400){
    64.             skillLevel += "Level Up!";
    65.         }
    66.         else if(coins >= 200){
    67.             skillLevel += "Level Up!";
    68.         }
    69.         else if(coins >= 100){
    70.             skillLevel += "Level Up!";
    71.         }
    72.         else if(coins >= 0){
    73.             skillLevel += "Level Up!";
    74.        
    75.        
    76.        
    77.         Rect labelRect = new Rect(coinIconRect.xMax, coinIconRect.y, 60, 32);
    78.         GUI.Label(labelRect, skillLevel);
    79.     }
    80. }
    81.  
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
  3. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    I solved that ... but now I have another problem ... Why it isn't displaying ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelPop : MonoBehaviour {
    4.      private int coins = 0;
    5.      public GUISkin levelPopSkin;
    6.  
    7.  
    8.  
    9.      // Use this for initialization
    10.      void Start () {
    11.      
    12.          coins = PlayerPrefs.GetInt("Coins", 0);
    13.      
    14.      }
    15.  
    16.      // Update is called once per frame
    17.      void Update () {
    18.      
    19.      }
    20.  
    21.      void FixedUpdate () {
    22.          PlayerPrefs.SetInt("Coins",coins);
    23.      }
    24.  
    25.      void OnGUI()
    26.      {
    27.          DisplayLevel ();
    28.      }
    29.  
    30.      void DisplayLevel()
    31.      {
    32.          GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,new Vector3(Screen.width / 480.0f, Screen.height / 320.0f, 1));
    33.          GUI.skin = levelPopSkin;
    34.          Rect coinIconRect = new Rect(200, 150, 32, 32);
    35.      
    36.          //LEVEL SHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    37.          string skillLevel = " ";
    38.      
    39.          if(coins >= 7500){
    40.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    41.          }
    42.          else if(coins >= 6900){
    43.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    44.          }
    45.          else if(coins >= 5600){
    46.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    47.          }
    48.          else if(coins >= 4500){
    49.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    50.          }
    51.          else if(coins >= 2300){
    52.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    53.          }
    54.          else if(coins >= 1600){
    55.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    56.          }
    57.          else if(coins >= 800){
    58.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    59.          }
    60.          else if(coins >= 400){
    61.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    62.          }
    63.          else if(coins >= 200){
    64.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    65.          }
    66.          else if(coins >= 100){
    67.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    68.          }
    69.          else if(coins >= 0){
    70.              StartCoroutine(ShowMessage("LEVEL UP !", 2));
    71.          }
    72.      
    73.      
    74.      
    75.      
    76.          Rect labelRect = new Rect(coinIconRect.xMax, coinIconRect.y, 60, 32);
    77.          GUI.Label(labelRect, skillLevel);
    78.      }
    79.  
    80.      IEnumerator ShowMessage (string message, float delay) {
    81.          guiText.text = message;
    82.          guiText.enabled = true;
    83.          yield return new WaitForSeconds(delay);
    84.          guiText.enabled = false;
    85.      }
    86. }
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Don't start a coroutine inside OnGUI(). OnGUI() is called several times per frame. Generally, in OnGUI() you should avoid checking and changing variables. Use it only for displaying GUI elements (and for checking Event.current if you implement GUI input).

    I see you're using GUIText. This means you don't need to use OnGUI() at all. Try something like this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelPop : MonoBehaviour {
    4.  
    5.   public GUIText guiText; // <-- Add a GUI Text to your scene and assign it here.
    6.  
    7.   private int coins = 0;
    8.   private int nextLevelCoins = 100;
    9.  
    10.   // Use this for initialization
    11.   void Start () {
    12.     coins = PlayerPrefs.GetInt("Coins", 0);
    13.     nextLevelCoins = coins + 100; // <-- Use a different formula if you want.
    14.     guiText.enabled = false;
    15.   }
    16.  
    17.   // Update is called once per frame
    18.   void Update () {
    19.     if (coins >= nextLevelCoins) {
    20.         StartCoroutine(ShowMessage("LEVEL UP!", 2));
    21.         nextLevelCoins += 100;
    22.     }
    23.   }
    24.  
    25.   void FixedUpdate () {
    26.     PlayerPrefs.SetInt("Coins",coins);
    27.   }
    28.  
    29.   IEnumerator ShowMessage (string message, float delay) {
    30.     guiText.text = message;
    31.     guiText.enabled = true;
    32.     yield return new WaitForSeconds(delay);
    33.     guiText.enabled = false;
    34.   }
    35. }
     
  5. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Don't work ...
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Post code?
     
  7. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    I used your's ...
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    It works. Here's a self-contained version that you can customize as needed. Just add it to your scene. For testing, it shows a button that gives you 10 coins. If you click it repeatedly, you'll level up.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelPop : MonoBehaviour {
    5.    
    6.     public GUIText myGuiText; // <-- Add a GUI Text to your scene and assign it here.
    7.    
    8.     public int coins = 0;
    9.     public int nextLevelCoins = 100;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.         coins = PlayerPrefs.GetInt("Coins", 0);
    14.         nextLevelCoins = coins + 100; // <-- Use a different formula if you want.
    15.         if (myGuiText == null) myGuiText = GetComponent<GUIText>();
    16.         if (myGuiText == null) myGuiText = gameObject.AddComponent<GUIText>();
    17.         myGuiText.enabled = false;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         if (coins >= nextLevelCoins) {
    23.             StartCoroutine(ShowMessage("LEVEL UP!", 2));
    24.             nextLevelCoins += 100;
    25.         }
    26.     }
    27.    
    28.     void FixedUpdate () {
    29.         PlayerPrefs.SetInt("Coins",coins);
    30.     }
    31.    
    32.     IEnumerator ShowMessage (string message, float delay) {
    33.         myGuiText.text = message;
    34.         myGuiText.enabled = true;
    35.         yield return new WaitForSeconds(delay);
    36.         myGuiText.enabled = false;
    37.     }
    38.  
    39.     public void OnGUI() {
    40.         GUILayout.Label("Coins: " + coins.ToString());
    41.         if (GUILayout.Button("Get 10 Coins")) {
    42.             coins += 10;
    43.         }
    44.     }
    45. }
     
  9. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    I just want when a certain number of coins is collected just show me a Level UP message and dissapear after 2 3 seconds ... ! that's all


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelPopUp : MonoBehaviour {
    4.  
    5.     private int coins = 0;
    6.     public GUISkin coinsSkin;
    7.     public GUISkin levelSkin;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.      
    13.         coins = PlayerPrefs.GetInt("Coins", 0);
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.      
    20.     }
    21.  
    22.     void FixedUpdate () {
    23.         PlayerPrefs.SetInt("Coins",coins);
    24.     }
    25.  
    26.     void OnGUI()
    27.     {
    28.         DisplayLevel ();
    29.     }
    30.  
    31.     void DisplayLevel()
    32.     {
    33.         GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,new Vector3(Screen.width / 480.0f, Screen.height / 320.0f, 1));
    34.         GUI.skin = levelSkin;
    35.         Rect coinIconRect = new Rect(200, 150, 32, 32);
    36.      
    37.         //LEVEL SHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    38.         string skillLevel = " ";
    39.      
    40.         if(coins >= 7500){
    41.             skillLevel += "Level Up!";
    42.         }
    43.         else if(coins >= 6900){
    44.             skillLevel += "Level Up!";
    45.         }
    46.         else if(coins >= 5600){
    47.             skillLevel += "Level Up!";
    48.         }
    49.         else if(coins >= 4500){
    50.             skillLevel += "Level Up!";
    51.         }
    52.         else if(coins >= 2300){
    53.             skillLevel += "Level Up!";
    54.         }
    55.         else if(coins >= 1600){
    56.             skillLevel += "Level Up!";
    57.         }
    58.         else if(coins >= 800){
    59.             skillLevel += "Level Up!";
    60.         }
    61.         else if(coins >= 400){
    62.             skillLevel += "Level Up!";
    63.         }
    64.         else if(coins >= 200){
    65.             skillLevel += "Level Up!";
    66.         }
    67.         else if(coins >= 100){
    68.             skillLevel += "Level Up!";
    69.         }
    70.         else if(coins >= 0){
    71.             skillLevel += "Level Up!";
    72.      
    73.      
    74.      
    75.         Rect labelRect = new Rect(coinIconRect.xMax, coinIconRect.y, 60, 32);
    76.         GUI.Label(labelRect, skillLevel);
    77.     }
    78. }
    79.  
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    That's what the code above does. Feel free to read it over to understand how it works, or leave it. Either way, best of luck!
     
  11. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Still don't work ... When I level up it not showing up any GUI ...