Search Unity

GAME OVER WITH SCORE?

Discussion in 'Scripting' started by SepticEmPire, Sep 2, 2018.

  1. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    Hi there! I am slightly new to making games, I wanted to create something that I am not sure on how to get around. I got my game made now, all I need is to polish it up and get this one thing in. I needed a game over menu when the character dies, but also with the score that the player has collected. The player collects points for killing enemies, but i am not sure on how to make the points show on the game over screen. Also, is there a way to save the score? For example a highscore?

    Thanks!
     
  2. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    Display player final score
    1. Design your game over panel or canvas the way you want in editor.
    2. Most likely you will want to have a separate Text object that displays player score only
    3. Create a script that has a reference to that Text object (you might add some code to the script that keeps track a player score so you won't need to create new script)
    4. Whenever player dies just update that Text object's value.
    Here is just a quick example. Make sure you drag Text object in unity editor. Call
    UpdateScore(int score)
    whenever you need it.
    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public Text scoreField;
    4.  
    5. public void UpdateScore(int score){
    6.      scoreField.text = score.toString();
    7. }
    It's just a random example how you can make this work. There are numerous of different approaches. I'm sure there are lot of youtube tutorials that works with UI.

    Save data to file
    There are a lot of posts about this. You can follow this one for example: https://answers.unity.com/questions/1300019/how-do-you-save-write-and-load-from-a-file.html
     
    sellim96 and SepticEmPire like this.
  3. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    Thank you very much!! It is slightly confusing for me since I am new to making games. I usually make 3D models, but I wanted to start creating my own small games :) Hopefully I can follow the instructions! Thank you once again :)
     
  4. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    Go to youtube and search for "brackey tower defence", it's a great tutorial series for a new developer as it covers various of different things. (Episode 17 in the series is about creating game over screen with user score)
     
    SepticEmPire likes this.
  5. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    I love Brackeys Channel! I followed his 2D platformer tutorial once :D Thank you, I will go check that out now :) Also, I just got the score thing to work on the game over screen so thank you so much!
     
  6. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    I ran in to another problem, not sure if it is my score code or this one but when I press my try again button to go back to the game, the score stays the same :(


    This is my other code, I put the scoring in with player death. It is a pretty simple script...


    using UnityEngine;

    public class EnemyDie : MonoBehaviour {


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    private void OnCollisionEnter(Collision collision)
    {
    if (collision.transform.tag == "Projectile")
    {
    scoresystem.scoreValue += 10;
    Destroy(gameObject);
    }
    }
    }





    Then this is the score screen...



    using UnityEngine.UI;
    using UnityEngine;
    public class ScoreScreen : MonoBehaviour {
    public Text scorefield;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void UpdateScore(int score)
    {
    scorefield.text = score.ToString();
    }
    }
     
    Last edited: Sep 2, 2018
  7. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    When Try again button is pressed you need to reset your score. You should be able to do it with the following added to try again button listener:
    Code (CSharp):
    1. scoresystem.scoreValue = 0;
    2. // and call UpdateScore(0) to update your UI
    3.  
     
  8. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    Sorry, how would I call updateScore? I am extremely new to coding, I don't understand that well haha. I just copy tutorials :p
     
  9. SepticEmPire

    SepticEmPire

    Joined:
    Jun 30, 2016
    Posts:
    9
    Oh I forgot, theres this bit too haha


    using UnityEngine;
    using UnityEngine.UI;

    public class scoresystem : MonoBehaviour
    {

    public static int scoreValue = 0;
    Text score;

    // Use this for initialization
    void Start()
    {
    score = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
    {
    score.text = "score: " + scoreValue;
    }
    }
    }
     
  10. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    Could you please write your code using code formatting (there is a button to add code when you are writing here in the comments)

    If you are completely new to coding I suggest you to create a couple of games from scratch following youtube tutorials. I can already see that your program architecture is not at its best. I'm not sure how much experience you have overall in programming (not game development), I believe not much. Don't start from games. You need a lot of different things to know. Start from simple console programs that simulates some real life situation. You need object-oriented programming basics.

    You can also re-think your whole project structure. What each script is responsible for? Is there any redundancy (yes, there is) ? And so much more.

    To answer your question: Since UpdateScore is not static method (do you know the difference between static and not? Google about it) you need to create an instance of your object (ScoreScreen) in the other where you want to use UpdateScore.

    Again highly suggest you to step back and start from the basic of object-oriented programming (OOP). When you know atleast the basics of OOP you will understand what I'm talking about. For now it's too much to explain in the forum.
     
    SepticEmPire likes this.