Search Unity

Setting up a score system to keep track of the score. How should I do this?

Discussion in 'Scripting' started by Joshhat98, Dec 17, 2014.

  1. Joshhat98

    Joshhat98

    Joined:
    Jul 28, 2013
    Posts:
    7
    Hi there,

    I am creating a endless runner, I'm almost done but require to add a score system, in which each item I jump I get one point.. I've been looking for the past two days for a tutorial on making system to keep track of Points/Score and I haven't found anything that works with the new unity UI.

    Basically I think (and hope) it would be set out that as my player collides with an invisible object above the item I am jumping over, I get one point. I'd also like to a highscore system, if you guys know much about all that.

    I am hoping, someone here can direct me to a tutorial in C# (Or easily convertible) or send some sample script with the set-up explained..

    Sorry for asking for help without even having some script, but I'm kind of clueless how to start.

    Thanks!
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Do you mean, with a database, so you can get the value for another time?
    Otherwise, implementation of a score variable with the unity UI is pretty straight forward:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ClickScore : MonoBehaviour {
    6.     Text text;
    7.     int score = 0;
    8.  
    9.     void Start () {
    10.         text = transform.Find("Text").GetComponent<Text>();
    11.     }
    12.    
    13.     void Update () {
    14.         if (Input.GetMouseButtonDown(0)) {
    15.             score++;
    16.             text.text = "SCORE: "+score;
    17.         }
    18.     }
    19. }
    20.  
    Example enclosed as unityPackage. I used "clicking" to get a score, rather than jumping, for brevity's sake.
     

    Attached Files:

  3. Joshhat98

    Joshhat98

    Joined:
    Jul 28, 2013
    Posts:
    7
    Hi there, Thanks for the reply!

    I'm still unsure on about how I'd make a script as when my player collides with an object, it adds to the score.. I understand I'd need two scripts?
     
  4. mubashar437

    mubashar437

    Joined:
    Sep 29, 2014
    Posts:
    28
    Code (CSharp):
    1. void OnCollisionEnter(Collision col){
    2. if(col.gameObject.tag == "object tag"){
    3. score++;
    4. }
    5.  
    6. }
    you could make score "public static" to access in any other script of game. this function goes onto Player...

    anyway google is ur best bet....search "unity collision scripting"
     
  5. Joshhat98

    Joshhat98

    Joined:
    Jul 28, 2013
    Posts:
    7
    May I ask, how exactly would I make the score a 'Public Static'? I'm guessing thats how I'd link these two scripts to work with each other?
     
  6. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    You are far from "almost done" if you don't understand that.

    http://unity3d.com/learn/documentation
     
  7. Kalaskrysset

    Kalaskrysset

    Joined:
    May 16, 2014
    Posts:
    9
  8. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Josh it sounds like you should do some scripting tutorials, learning the concepts behind programming would definitely make development easier for you.

    That being said, specifically what you're looking for is the OnCollisionEnter event, as mubashar pointed out.

    I whipped up a quick example:
     

    Attached Files: