Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Variable shown with GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by Sigvard, Mar 11, 2010.

  1. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    I am building a very simple, cartoony FPS shooter and i want a text to be in the corner, telling the player his remaining number of bullets and clips, but my scripting is.. well it sucks to be honest.

    This is my javascript so far:

    Code (csharp):
    1.  
    2. var bullets:float;
    3. var clips:float;
    4. var shootspeed:float;
    5. var bulletPrefab:Transform;
    6.  
    7. function Update ()
    8.  
    9. {
    10.    
    11.     if(Input.GetButtonDown("Fire1")){
    12.  
    13.         if (bullets > 0){
    14.         bullets --;
    15.         var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnpoint").transform.position, Quaternion.identity);
    16.         bullet.rigidbody.AddForce(transform.forward*shootspeed);
    17.  
    18.         audio.Play();
    19.         }
    20.  
    21.     }
    22.    
    23.     if(Input.GetButtonDown("Reload")){
    24.        
    25.         if (clips > 0){
    26.         bullets = 30;
    27.         clips --;
    28.         }
    29.     }
    30. }
    31.  
    Remember that my scripting is poor so keep it simple please :D Thx
     
  2. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
  3. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    I've already looked at that, but i cant seem to figure out how to pass in a variable as text.
    lets say i use the test GUI:
    Code (csharp):
    1.  
    2. function OnGUI () {
    3.     GUI.Label (Rect (25, 25, 100, 30), "Label");
    4. }
    5.  
    What do i need to write as "Label" to show the value of my "var bullets" or "var clips"?
     
  4. Mat

    Mat

    Joined:
    Dec 4, 2009
    Posts:
    30
    You can either use the ToString() method (I'm not sure if it's called like this in Javascript) of concatenate your variable with a string, e.g.:

    Code (csharp):
    1. var bullets : int;
    2.  
    3. function OnGUI () {
    4.     bullets = 10;
    5.  
    6.  
    7.     GUI.Label( myRect, bullets.ToString() ); // Displays "10".
    8.  
    9.     GUI.Label( myRect, "" + bullets ); // Displays "10".
    10.  
    11.     GUI.Label( myRect, "Ammo: " + bullets + " bullets remaining." ); // Displays "Ammo: 10 bullets remaining.".
    12. }
     
  5. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    Thx :D that was what i needed.
    I ended up with adding this to my script and i works as a charm :)
    Code (csharp):
    1.  
    2. function OnGUI () {
    3. GUI.Label (Rect (10, 10, 100, 20),"bullets left: " + bullets);
    4. }
    5.