• 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 sowmi97 · Jul 20, 2020 at 10:03 AM · 3dscore

How do I keep the score when the scene is reloaded?

Hi, I'm new to Unity and I'm trying to build a basketball game. So I have the MainScene which gets reloaded after every attempt at a shot and brings the ball and the player back to its initial position so that the player can try again. But every time this happens, the score goes back to 0. I would like it to get updated as long as the play button is on. How do I do that? I tried using PlayerPrefs but I think I'm doing it wrong. I have two scripts, one which is attached to the basketball ring to activate the box collider placed below it to count as one goal and another to a game controller which is an empty game object that reloads the scene after an attempt to shoot.

Script attached to ring (Basket.cs):

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class Basket : MonoBehaviour { public GameObject score;

 public void OnTriggerEnter()
 {
     PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score") + 1);
     score.GetComponent<UnityEngine.UI.Text>().text = PlayerPrefs.GetInt("Score").ToString();
 }

}

Script attached to GameController: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.SocialPlatforms.Impl;

public class GameController : MonoBehaviour { public ThrowBall player; public float resetTimer = 5f; public Basket m_Basket; public GameObject score;

 private void Awake()
 {
     PlayerPrefs.SetInt("Score", int.Parse(score.GetComponent<UnityEngine.UI.Text>().text));
 }
 void Update()
 {
     if (player.holdingBall == false)
     {
         resetTimer -= Time.deltaTime;
 
     }
     if (resetTimer <= 0)
     {
         m_Basket = GameObject.FindObjectOfType(typeof(Basket)) as Basket;
         m_Basket.OnTriggerEnter();
         SceneManager.LoadScene("MainScene");
     }
 }

}

What am I doing wrong? How can I fix this?

Comment
Add comment · Show 1
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 frederikedel · Jul 20, 2020 at 11:22 AM 0
Share

Another option you could use is DontDestroyOnLoad $$anonymous$$ethod, so the script will not be destroyed even though you switched the scene.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Acdia · Jul 20, 2020 at 03:07 PM

If I see this correctly you are overwriting your safed value in the Awake() method of your GameController. Awake is called every time you load the scene, so it should be resetted after each throw. You could maybe do the resetting in some menuScene. (Or use frederikedel's solution)

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 sacredgeometry · Jul 20, 2020 at 12:19 PM

You need to persist the game state (and then load it in on scene load) somehow.

There are many ways to do that, the built in way is to use PlayerPrefs

https://medium.com/@zachcaceres/persistent-data-between-scenes-how-to-use-playerprefs-in-unity-b6fd409363c3


But you could also use a database, or serialise to either a binary file or some sort of structured file format like JSON, XML, CSV, EXCEL or even plain text.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

176 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D ^ Custom GUIText/Label or Making a Score System without using GUI? 0 Answers

How i can calculate points when hitting dart target? 2 Answers

How would I make a score board or how many enemies I killed? 1 Answer

3D character mesh ignoring transform values and rotating without the rig 0 Answers

Does unity support flatsprite? 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