Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Score system?

Discussion in 'Scripting' started by Tereith2050, Sep 9, 2015.

  1. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    I'm currently making a game where you controll 1 cowboy and have to shoot another. Like the old atari and comadore game Gun fight


    Everything works perfectly and thanks to everyone who helped me so far getting this dream of a game comming true!! as i no longer own a comadore to play it :(

    My game is pretty much soon done.

    I only need 1 last thing and that is what i call the hardest part of whole this game.
    The score system.

    I want a text saying P1 that's easy. And next to that i want a score system

    Like this



    I want the score to update whenever the bot kills me and he gets 1 point or when i kill the bot and i get 1 point. When either one gets 1 point you /respawn the first guy to 20 wins.

    Can someone help me. I'm on bare ground here and have no idea what to do here.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    script which holds the two scores, and has an "increase score x by value y" function. This needs to be on a gameobject that isn't destroyed during the game (called ScoreManager or something).

    The scoring event (I'm assuming a use of raycasts or oncollision functions?) need to be able to say ScoreManager.IncreaseScore(x), so that script will either need to find the scoremanager go/script or be given a reference to it.


    You then update the canvas elements from scoremanager.
     
  3. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    So like this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Score : MonoBehaviour {
    4. int score = 0;
    5. public void AddToScore () {
    6.     ++score;
    7.     guiText.text = "Score: " + score.ToString ();
    8. }
    9.  
    10.     }
    ??

    I just create a new script called

    Score

    Attach that to a empty gameobject which holds the score text gui?
     
  4. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    Add this script to Player and enemy:

    Code (CSharp):
    1. public class ScorePoint : MonoBehaviour {
    2.     public int scoreValue = 1;  
    3.     void OnTriggerEnter(Collider collider) {
    4.         if(collider.tag == "Bullet") {
    5.             scoreManazer.score += scoreValue;
    6.         }
    7.     }
    8. }
    9.  
    And this script add to UI text

    Code (CSharp):
    1. public class scoreManazer: MonoBehaviour {
    2. public static float score;
    3.  
    4. void Update(){
    5. GetComponent <Text> ().text = score + " Player";
    6.  
    7. if (score => 20){
    8. Restart();
    9. }
    10. }
    11. void Restart(){
    12. Application.LoadLevel(0);
    13. }
    This is not tested but it should works...
     
  5. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Thanks Johny. Will check it out straight away! :)
     
  6. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
  7. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    OOOHHH forget it. I'm stupid.i hadn't created the other script and that's why. :D
     
  8. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    In scoreManazer
    dont use "public static float score"
    use "public static int score"
     
  9. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Did.

    What i did
    Added the ScorePoint Script to the player and enemy

    Created a new Gameobject - UI - Text
    Created the new scoreManazer script but can't add it to the text yet hence this



     
  10. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    It's just the Get component line there's wrong. I already have that UI - Text out in my scene. It's a child of a canvas which was created when i created that Gameobject - ui - text. Hmmmmm
     
  11. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    here is it:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI; //THIS!!
    3. using System.Collections;
    4.  
    5.  
    6. public class scoreManazer: MonoBehaviour {
    7.     public static int score;
    8.     private float MaxScore;
    9.     Text text;
    10.  
    11.     void Awake ()
    12.     {
    13.         // Set up the reference.
    14.         text = GetComponent <Text> ();
    15.  
    16.     }
    17.  
    18.     void Update(){
    19.         text.text = "Player " + score;
    20.      
    21.         if (score >= MaxScore){
    22.             Restart();
    23.         }
    24.     }
    25.     void Restart(){
    26.         Application.LoadLevel(0);
    27.     }
    28. }
    29.  
    30.  
    You must use UnityEngine.UI
     
  12. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Thanks everything seems to be fine without any errors.

    So now i just add this scoreManazer script to my Text?
     
  13. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    Yes only add scoreManazer to text and Scorepoint add to player... check player and bullets colliders
     
  14. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're also going to need to change it to handle two scores, the player's and the bot's
     
  15. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Is this the correct text i'm using?

     
  16. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    I changed Score = maxscore to 20. as well.

    Here's the text

     
  17. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    There are errors in the scoremanazer script... what errors is it throwing
     
  18. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    this :)

     
  19. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    I changed it to a Uppercase S again. Problem is can't change it by it's name because it keeps going back to a uppercase.
     
  20. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Fixed it.

     
  21. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Ok we're pretty close. It shows a 0 as it's default value now i just need it to change it so that when i kill the bot it changes to 1 and 2 and 3 and 4 etc.




    Top left corner.
     
  22. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    Are you added ScorePoint to Player and enemy? Are you checked collider?
     
  23. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Yes i use a 2D collider for both the players and bullets.
     
  24. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Does a 2d collider not work with that script or what?. Everything is good. I just need the numbers to go up by +1 for each kill.
     
  25. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Hmm could it be because it's not set to + 1 score? or what

     
  26. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    It's adding scorevalue from ScorePoint script... you must set up correctly colliders,triggers, tags... bullets must have trigger collider with "Bullet" tag
     
  27. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Both players have 2D colliders. The bullets have a 2d collider as well. The bullet is tagged Bullet

    This is the full ScorePoint script



    Idk what else could be wrong. I'm literally out of ideas.
     
  28. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    If you are using 2D collider set OnTroggerEnter2D (Collider2D collider)
    do you have rigidbody on bullet or player? In triggering you must have rigibody
     
  29. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    I have a rigidbody2d on both the player and the bullet.
    The bullet is ticked as trigger in 2d collider gravity scale is at 0 so it won't just drop.

    Also the player is a rigidbody2D
    set to 0 as well.
    But is not ticked as a trigger.

    I'm gonna try and make the change.
     
  30. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Great it's actually working now. 1 thing. When i shoot it adds 1 point. For every shot i make.

    Like 1 shot = +1 point.

    I want it

    1 kill = +1 point
     
  31. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    These 2 are my full scripts now

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI; //THIS!!
    3. using System.Collections;
    4.  
    5.  
    6. public class scoreManazer: MonoBehaviour {
    7.     public static int score;
    8.     private float MaxScore;
    9.     Text text;
    10.    
    11.     void Awake ()
    12.     {
    13.         // Set up the reference.
    14.         text = GetComponent <Text> ();
    15.        
    16.     }
    17.    
    18.     void Update(){
    19.         text.text = "Player " + score;
    20.        
    21.         if (score >= 20){
    22.             Restart();
    23.         }
    24.     }
    25.     void Restart(){
    26.         Application.LoadLevel(0);
    27.     }
    28. }
    29.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScorePoint : MonoBehaviour {
    5.     public int scoreValue = 1;
    6.     void OnTriggerEnter2D(Collider2D collider) {
    7.         if(collider.tag == "Bullet") {
    8.             scoreManazer.score += scoreValue;
    9.         }
    10.     }
    11. }
    12.  
     
  32. Tereith2050

    Tereith2050

    Joined:
    Sep 6, 2015
    Posts:
    78
    Wished i learn'd scripting.. Help!!