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 22, 2019 at 01:22 PM · positionscore

How to keep position based score to next scene

The score of my game is based on the position of the player in the z axes, but i can't figure out how to keep that score to next scene and start adding up.

Score Script

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
 public class Score : MonoBehaviour
 {
 
     public static 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);
         }
      
     }
 }

GameManager Script

 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);
 
     }
 }


I have tried lots of things like DontDestroyOnLoad() or making the float static, and other... Does the fact that my score is based on position means that i can't keep it in the next scene?

Comment
Add comment · Show 5
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 Ymrasu · Feb 22, 2019 at 01:51 PM 0
Share

I see you are using PlayerPrefs to save the score. If you want to use PlayerPrefs.GetFloat in the new scene to keep adding up, you have to change how you are saving it. You are currently saving only your current zPos. You'll need an extra variable like totalScore and keep adding to that. Then set and get that between scenes.

avatar image poupakos1 Ymrasu · Feb 22, 2019 at 04:18 PM 0
Share

But how can you calculate them as : "previous Position " and "Current Position" at the same time? This is my noobish attempt

     public Transform player;
     public Text scoreText ;
     float zPos;
     static float totalScore;
     public int sceneCounter;
     void Update()
     {
         sceneCounter = Scene$$anonymous$$anager.GetActiveScene().buildIndex;
         zPos = player.position.z;
         scoreText.text = totalScore.ToString("0");
         PlayerPrefs.SetFloat("theScore", zPos);
         totalScore = zPos;
 
         if (sceneCounter == sceneCounter + 1)
         {
            totalScore = totalScore + PlayerPrefs.GetFloat("theScore");
         }
 


Note: The answer might be simple and clear but i am new to c# with unity

avatar image Ymrasu poupakos1 · Feb 22, 2019 at 04:29 PM 1
Share

You can use Start() to get what the previous score was when starting a new scene. float previousScore

 void Start()
 {
     previousScore = PlayerPrefs.GetFloat("theScore");
 }

Then you can get the totalScore by adding previousScore and zPos.

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sinister-Design · Feb 22, 2019 at 09:48 PM

What you want is a static class that you can use to store (and access) data globally, regardless of scene. Something like:

 public static class GlobalData {
 
     public static float score = 0;

     public static void incrementScore ( float amount ) {
         score += amount;
     }
 
     public static float getScore () {
         return score;
     }
 }


Once you've created this class in a script file, you can easily update the score by calling GlobalData.incrementScore(), and retrieve it by calling GlobalData.getScore() in the next scene.

(To be clear: your existing Score class does not serve this function because it is not static, and extends MonoBehaviour. To use a class like this with your existing scripts, you'd probably want to make the NextLevel() method call GlobalData.incrementScore( Score.zPos ); right before it calls the SceneManager.)

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

124 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

Related Questions

Camera rotation around player while following. 6 Answers

Check GameObject postion 1 Answer

Click object and it will randomly change its position + add score 0 Answers

How do i add points when the object past a specific coordinate 1 Answer

Networking: How do I send player positions? 2 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