Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 10, 2018 at 06:39 PM by $$anonymous$$ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by $$anonymous$$ · Aug 10, 2018 at 01:10 AM · scenescore

How do I make score appear when game is over and record high score?

Hello everyone, I'm new to unity. I'm working on an endless running 2D game. I have successfully added score in the game. Now I want the score to appear when the game is over. I have made the settings so when the game is over new scene is loaded. This is where I want the score made by player to appear. I also want the high score there. Help will be appreciated. Thanks.

EDIT: This is the score code in Player script:

      ...
      public int score = 0;
      public Text scoreText;
  
      void Start() {
          ...
          scoreText.text = score.ToString();
      }
  
          private void OnTriggerEnter2D(Collider2D collision)
          {
              if (collision.tag == "Score")
              {
                  score++;
                  MusicSource.Play();
                  Destroy(collision.gameObject);
                  Instantiate(obstacle, new Vector2(transform.position.x, transform.position.y + 8f), transform.rotation);
                  return;
              }
      
              {
                  Debug.Log("You Died");
                  SceneManager.LoadScene(2);
              }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by MT369MT · Aug 10, 2018 at 01:38 PM

Hi, you could use a static variable. Static variables will be always the same in all scripts in your scene and if you change scene they will not change the value.

To decleare a static variable write it so:

 public static int score;
 public static int highScore;


Then create two new UI Texts and name them scoreText and highScoreText. In the script create two Text variables and then assign them in inspector or in script.

 public Text scoreText;
 public Text highScoreText;

Remember that you need to add using UnityEngine.UI; at the top of your script to access to the UI components.


The high score will only be changed if the score is greater then the current score. To do it simply add an if condition.

 if (score > highScore)
 {
 highScore = score;
 }

Then change the text value of your UI Texts:

 scoreText.text = score.ToString();
 highScoreText.text = highScore.ToString();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by $$anonymous$$ · Aug 10, 2018 at 04:07 PM

@MT369MT Should I create a new script in the new scene? This how I have added score in the Player script:

     ...
     public int score = 0;
     public Text scoreText;
 
     void Start() {
         ...
         scoreText.text = score.ToString();
     }
 
         private void OnTriggerEnter2D(Collider2D collision)
         {
             if (collision.tag == "Score")
             {
                 score++;
                 MusicSource.Play();
                 Destroy(collision.gameObject);
                 Instantiate(obstacle, new Vector2(transform.position.x, transform.position.y + 8f), transform.rotation);
                 return;
             }
     
             {
                 Debug.Log("You Died");
                 SceneManager.LoadScene(2);
             }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MT369MT · Aug 10, 2018 at 06:17 PM 1
Share

Yes, you could create a new script called for example Score, and you can attach it to an empty GameObject in your Score scene.

Remember to add static to your score variable in your player script.


Here the complete Score script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : $$anonymous$$onoBehaviour {
 
     public static int score;
     public static int highScore;
 
     public Text scoreText;
     public Text highScoreText;
 
     // Use this for initialization
     void Start () {
 
         scoreText = GameObject.Find("scoreText").GetComponent<Text>();
         highScoreText = GameObject.Find("highScoreText").GetComponent<Text>();
 
         score = Player.score;   //Ins$$anonymous$$d of Player use the name of the script with the score
 
         if (score > highScore)
         {
             highScore = score;
         }
 
         scoreText.text = "Score: " + score.ToString();
         highScoreText.text = "High Score: " + highScore.ToString();
 
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 }
avatar image $$anonymous$$ MT369MT · Aug 10, 2018 at 06:38 PM 0
Share

It worked. Thanks a lot.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

change scene by score 0 Answers

How to show score another scene ? And reset the score when the restart scene? 2 Answers

How do I make scenes switch properly c# 0 Answers

How do switch scene with singleton pattern unity3d C# 2 Answers

How do I switch scenes in a else statement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges