Search Unity

Score Gui

Discussion in 'Scripting' started by The3DKnight, May 29, 2012.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi all i have one question.
    I've got timer count down script and score script.But how can i add like when my time is 0 that score from my score script shows on screen ?Thank you.
    Here are scripts.


    Timer Script
    Code (csharp):
    1. private var startTime;
    2. private var restSeconds : int;
    3. private var roundedRestSeconds : int;
    4. private var displaySeconds : int;
    5. private var displayMinutes : int;
    6.  
    7. var countDownSeconds : int;
    8.  
    9. var mySkin : GUISkin;
    10.  
    11. function Awake() {
    12.     startTime = Time.time;
    13. }
    14.  
    15. function OnGUI () {
    16.  
    17.     GUI.skin = mySkin;
    18.    
    19.     var guiTime = Time.time - startTime;
    20.  
    21.     restSeconds = countDownSeconds - (guiTime);
    22.  
    23.    
    24.     if (restSeconds == 60) {
    25.         print ("One Minute Left");
    26.     }
    27.     if (restSeconds == 0) {
    28.         print ("Time is Over");
    29.         //do stuff here
    30.     }
    31.  
    32.     roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    33.     displaySeconds = roundedRestSeconds % 60;
    34.     displayMinutes = roundedRestSeconds / 60;
    35.  
    36.     text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
    37.     GUI.Label (Rect (400, 25, 100, 30), text);
    38. }
    And here's my Score script...

    Code (csharp):
    1. var mySkin : GUISkin;
    2. static var Counter : int;
    3.  
    4.  
    5. function OnGUI()
    6. {
    7.     GUI.skin = mySkin;
    8.     var GuiSkin : GUISkin;
    9.     GUI.Label (Rect (10, 10, 100, 20), "Score: " + Counter);
    10. }
    11.  
    12. function OnTriggerEnter(other : Collider)
    13. {
    14.     if(other.tag == "the tag for your player object")
    15.     {
    16.         GuiScript.Counter += 1;
    17.     }
    18.  
    19. }
     
  2. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    Are you accessing the variable "Counter" from another script? If not, then remove "static" and use "private" instead, to make it un-editable in the inspector.

    You may also want to make the second function of your score code more along the lines of:

    Code (csharp):
    1.  
    2. private var Counter: int;
    3.  
    4. function OnTriggerEnter(other: Collider) {
    5.      if (other.tag == "the tag for your player object") {
    6.           Counter = Counter + 1;
    7.      }
    8. }
    9.  
    Let me know if that works.
     
  3. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Yes i'am accessing 'Counter' from other scripts.But my question is how can i make my final score show after time runs out.Any help? Thank you.
     
    Last edited: May 30, 2012
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    function onGui()
    {
    if( time == 0)
    displayScore();
    }
     
  5. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Thank You but how can i make code from this script use the score from another script?
     
  6. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    I think your best and simplest bet would be to combine the scripts and do it that way. The only time you might not want to is if you disable one script but want/need the other to keep working.
     
    Last edited: May 30, 2012
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Read this and then read this and lastly read this.
     
  8. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Ok guys thanx a lot!!!But i've got one more problem.It's that when my timer gets to 0.The score appears but only for few second then it goes -1,-2,-3,-4....And the Score text disappears.Ooo(and i joined the scripts)...Thank you for your time :D
     
  9. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You would want to stop your timer when it reaches 0.
    Simple condition.
     
  10. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I know i added line:
    Code (csharp):
    1. if (restSeconds == 0) {
    2.         print ("Time is Over");
    3.      
    4.        
    5.         //do stuff here
    6.         GUI.Label (Rect (100, 100, 100, 100), "Score: " + Counter);
    7.        
    8.         restSeconds.Stop();
    9.        
    10.     }
    "restSeconds.Stop();"

    but it stops for a sec. But it somehow goes to - and hides Score?
    Any help?Thank you ;)
     
  11. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Ok srry i got it.But if someone needs it just change from
    to
    Thanx any way....
     
  12. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    If you have any trouble with it, Try if(restSeconds<=0)

    That should stop it at 0 and not -1
     
  13. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    how can i add seconds from one script to timers script.Something like
    Code (csharp):
    1. function OnMouseOver()
    2. {
    3.  if(Input.GetMouseButtonDown(0))
    4.  {
    5.     Destroy(gameObject);
    6.   GuiScript.restSeconds += 50;
    7.  }
    8. }
    9.  

    Timer Script is Up^
    Thank you.. :)
     
  14. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
  15. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Really?Nobody?
     
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're basically asking the same question you asked before...
     
  17. SmokeTear

    SmokeTear

    Joined:
    Aug 18, 2012
    Posts:
    18
    The3DKnight

    Use this for your score :

    Attach this on a your Enemy - as a separate script
    ---------------------------------------------------

    function OnCollisionEnter(boom : Collision) {

    if(boom.gameObject.tag == "Bullet")
    {
    life +=1;
    if(life == 5)
    Destroy(gameObject);
    }
    }

    function OnDestroy() {
    ScoreScript.score += 100;
    }
    ----------------------------------------------------
    *Notice that this script above makes your enemies have Life*
    -----------------------------------------------------
    So basicaly what this up here ^^ means if the enemy is destroyed then your score goes up by 100



    And this on any GameObject
    ----------------------------------------------------
    static var score : int = 0;
    var myFont : Font;

    function OnGUI() {
    GUI.skin.font = myFont;
    GUI.Label(Rect(0, 1, 200, 40), "Score:" + score);
    }



    And if you want a complete EnemyAI Script that follows the player, has a life and die function tell me ill reply :D
     
  18. SmokeTear

    SmokeTear

    Joined:
    Aug 18, 2012
    Posts:
    18
    After the :
    if (restSeconds == 0) {

    print ("Time is Over");
    }



    Type in Awake();
    so it will be like this

    if (restSeconds == 0) {

    print ("Time is Over");
    Awake();
    }


    This will make the script run again and again reapeating like a loop