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 /
avatar image
0
Question by poupakos1 · Feb 21, 2019 at 07:49 PM · score system

How to keep track of score between Scenes?

This problem is really bugging me, any help would be appreciated!

Script #1

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
 public class Score : MonoBehaviour
 {
 
     public Transform player;
     public Text scoreText ;
     static float zPos;
 
 
 
     void Update()
     {
 
         zPos = 1 + player.position.z / 2;
         scoreText.text = zPos.ToString("0");
         PlayerPrefs.SetFloat("theScore", zPos);
 
 
         
         if (FindObjectOfType<GameManager>().GameEnds == true)
         {
             Destroy(scoreText);
         }
      
     }
 }


Script #2

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 
 public class GameManager : MonoBehaviour
 {
     public bool GameEnds = false;
 
     public GameObject levelComplete;
 
     public GameObject GameOverAn;
 
     public GameObject pauseMenuUI;
 
     public static bool pauseMenu;
 
     public void GameOver()
     {
 
 
         if (GameEnds == false)
         {
             GameEnds = true;
             GameOverAn.SetActive(true);
             Destroy(levelComplete);
 
         }
     }
 
     public void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if (pauseMenu)
             {
                 Resume();
             }
             else
             {
                 Pause(); ;
             }
 
         }
 
     }
 
     public void Resume()
     {
         pauseMenuUI.SetActive(false);
         Time.timeScale = 1f;
         pauseMenu = false;
     }
 
     void Pause()
     {
         pauseMenuUI.SetActive(true);
         Time.timeScale = 0f;
         pauseMenu = true;
     }
 
     void RestartGame()
     {
 
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 
     }
 
     public void NextLevel()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
 
     public void LevelComplete()
     {
 
         levelComplete.SetActive(true);
         Destroy(GameOverAn);
 
     }
 }


Note: I am a beginner casually looking for answers from similar questions, but nothing that i found was helpful (since i am not very familiar with complicated code).

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jstopyraIGG · Feb 21, 2019 at 08:06 PM

You can tell Unity not to delete certain objects when scenes are loaded and unloaded

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

 //this game object will not get destroyed between scene loading
 void Awake()
 {
     DontDestroyOnLoad(gameObject);
 }


You have to be careful with using this. Because if an object is not destroyed, you might get duplicates of it, because a new one will get spawned when a scene loads. You might want to use Singletons.

https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager

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 poupakos1 · Feb 21, 2019 at 08:40 PM

But the score is based on the position of z axes , so either way it'll go back to 0, because my player always starts at position 0 of the z axes.

Comment
Add comment · Show 1 · 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 jstopyraIGG · Feb 21, 2019 at 09:07 PM 0
Share

I see, All you have to do is load the score

 Vector3 playerPos = player.position;
 playerPos .z = PlayerPrefs.GetFloat("theScore", zPos);
 player.position =  playerPos ;
avatar image
0

Answer by telecaster · Feb 04 at 08:30 PM

I know I might get alot of hate for this, but could a singleton be the answer? https://gamedevbeginner.com/singletons-in-unity-the-right-way/

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

How to increase score by one per second when you are holding an object 2 Answers

Displaying Score 1 Answer

Changing the UI text value from server to Client side 0 Answers

adjustable point limit 1 Answer

How do I stop adding to a variable? 1 Answer


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