• 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 burchland2 · Nov 30, 2022 at 08:53 PM · reset-state

Reset Player Statistics after winning game

I have a big problem and I was just looking for a simple answer. I want to reset the player statistics to default (Score to zero and Lives to 9 for example) after winning the game, going back to the title scene and then to the first level again.

How do I do that? Here is the relevant code:

GameStatus:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameStatus : MonoBehaviour
 {
     static public int lives = 9;
     static public int score = 0;
 
     public AudioSource source;
 
     public AudioClip bell;
 
     public static int target = 100;
 
     public PlayerController ball;
 
     public HealthManager hm;
 
     public GameObject gameOverScreen;
 
     public GameObject currentCheckpoint;
 
     public CountdownTimer ct;
 
     // Start is called before the first frame update
     void Start()
     {
         source = GetComponent<AudioSource>();
         gameOverScreen.SetActive(false);
         ball = FindObjectOfType<PlayerController>();
         hm = FindObjectOfType<HealthManager>();
         ct = FindObjectOfType<CountdownTimer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (lives <= 0)
         {
             gameOverScreen.SetActive(true);
             ball.gameObject.SetActive(false);
         }
     }
 
     public void AddScore(int s)
     {
         score += s;
         w$$anonymous$$le (score >= target)
         {
             AddLives(1);
             target += 100;
             source.PlayOneShot(bell);
         }
     }
 
     public void ScoreMultiply()
     {
         int multiScore = 10;
         score += multiScore;
         w$$anonymous$$le (score >= target)
         {
             AddLives(1);
             target += 100;
             source.PlayOneShot(bell);
         }
     }
 
     public void AddLives(int l)
     {
         lives += l;
     }
 
     public void LoseLives()
     {
         lives--;
     }
 
     void OnDestroy()
     {
         Debug.Log("'GameStatus' was destroyed");
     }
 
     public void Reboot()
     {
         score = 0;
         lives = 9;
         gameOverScreen.SetActive(false);
         ball.gameObject.SetActive(true);
         ct.ResetTime();
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 
     public void Reset()
     {
         GameManager.instance.score = 0;
         GameManager.instance.lives = 9;
     }
 }

And the problem lies here: GameManager:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.IO;
 
 public class GameManager : MonoBehaviour
 {
     public static GameManager instance;
 
     public bool isPaused;
 
     public Text scoreText, livesText;
 
     public int score = 0;
 
     public int lives = 9;
 
     void Awake()
     {
         if(instance == null)
         {
             instance = t$$anonymous$$s;
         }
         else
         {
             if(instance != t$$anonymous$$s)
             {
                 Destroy(gameObject);
             }
         }
         DontDestroyOnLoad(gameObject);
     }
 
     void Update()
     {
         scoreText.text = score.ToString();
         livesText.text = lives.ToString();
     }
 }

Apparently, I want to keep the DontDestroyOnLoad method during gameplay, but also want to reset the game stats after the game is reset. Can anyone tell me how to do t$$anonymous$$s? Thank you very much.

Sincerely, burchland2

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Dec 01, 2022 at 12:59 AM

  1. Create a player stats class

  2. Initialise it properly either with default values or by writing a constructor

  3. Create an instance of that type and store a reference to it somewhere, maybe on a gamemanager class/ monobehaviour

  4. When you win just set that reference to a new instance of the player stat class


That would be the easiest way IMHO, the GC overhead should be relatively negligible. If you dont want to do that then write a method that just sets all the values to what you want their default values to be and call it.

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 burchland2 · Dec 02, 2022 at 09:57 PM 0
Share

How do I set the reference from an instance of one class to a new instance of a different class? Once I set an instance from GameManager to a new instance of GameStatus, I get an error: Cannot implicitly convert type 'GameManager' to 'PlayerStatistics'.

Here is what I did for step 4: ps.instance = instance;

T$$anonymous$$s step is a little confusing to me. Can you please refresh my memory as I recently took a Game Development course?

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

146 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

Related Questions

Reset Save Data 1 Answer

How to reset some specific game objects in the scene via a button? 2 Answers

Reset public float to original value after reaching cursor 1 Answer

How do I reset all static variables at the end of the game? 1 Answer

Resetting GameObjects After Dying 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