Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zaditen · Sep 14, 2020 at 02:22 AM · playerprefsstart

Can't restart my game after game over

Hello Unity Community! I ran into some problems that i just can't figure out on my own i realized after hours of searching. So here is my problem

I have playerprefs keeping track on my health and score. after my health gets to 0 you get back to the startscene (main menu) and playerprefs get deleted ( i assume? ). now the problem is when i press play again in the menu, the game starts for a second and then i get back into the menu again. I think that the problem is my game thinks my health is still 0 so it returns me to the main menu, but i can't figure out how to do it. here is my code. atleast for my gamemaster script that handles it:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
 public class gameMaster : MonoBehaviour
 {
     public int score;
     public Text scoreText;
     public Text InputText;
     public int curHealth;
     public int maxHealth = 4;
 
 
     private PlayerController player;
 
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
 
         Scene currentScene = SceneManager.GetActiveScene();
 
         string sceneName = currentScene.name;
 
         
 
         
 
         if (PlayerPrefs.HasKey("Score"))
         {
             if (sceneName == "StartScene")
             {
                 PlayerPrefs.DeleteKey("Score");
                 score = 0;
             }
             else
             {
                 score = PlayerPrefs.GetInt("Score");
             }
         }
 
 
         if (PlayerPrefs.HasKey("Health"))
         {
             if (sceneName == "StartScene")
             {
                 PlayerPrefs.DeleteKey("Health");
                 curHealth = 0;
                 
             }
 
             else
             {
 
                curHealth =  PlayerPrefs.GetInt("Health");
                 
 
 
 
             }
 
 
         }
 
 
 
         }
 
     private void Update()
     {
         
         scoreText.text = ("X " + score);
         
 
         if (curHealth > maxHealth)
         {
             curHealth = maxHealth;
 
         }
         if (curHealth <= 0)
         {
             Die();
         }
 
         if (score >= 100)
         {
             GiveHealth();
         }
     }
 
     
 
 
 
     void GiveHealth()
     {
 
         if (curHealth == maxHealth)
         {
 
             score = 0;
 
         }
 
         else
         {
             score = 0;
             curHealth += 1;
 
         }
     }
 
     public void TakeDamage(int damage)
     {
         curHealth -= damage;
     }
 
 
     private void Die()
 
         {
  
 
         SceneManager.LoadScene("StartScene");
 
         }
             
         
 
 
     
 
 
     
 
 }

now i've tried alot of different things to change this. for example i tried in my Die method to give health a new value after the startscene is loaded. i tried searching if i could make a new player pref after the old one was deleted with a new value so it doesn't stay at 0 so i can't start my game, but i have no idea how to solve this. can anyone help me?

cheers

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 Capricornum · Sep 14, 2020 at 07:21 AM 0
Share

A few ideas of the top of my head.

You can put a Debug.Log message in your Die() method to check whether it is called once you click on play in the main menu. You can also put a debug.Log message in your PlayerPrefs.DeleteKey if statement to check whether is actually called. Also put a Debug.Log message in start to check what the actual sceneName is. $$anonymous$$aybe you have some hiccup with the active scene and StartScene is never the active one.

Is your game$$anonymous$$anager a prefab instance that you put in your game scene and your main menu? Because if you have it in your game scene only and it doesn't exist in the main menu (or StartScene) then the PlayerPrefs deletion will obviously never happen.

Oh and most importantly. Once you are deleting the PlayerPrefs.Health key in StartScene you then make Health = 0. That means your health is now 0. That means when you load the game scene your health is 0 and you die. So rh_galaxy suggests the solution here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Sep 14, 2020 at 07:04 AM

Something like this, but I can't see where you set PlayerPrefs or where you load other scenes than the StartScene:

 public class gameMaster : MonoBehaviour
 {
     public int score;
     public Text scoreText;
     public Text InputText;
     public int curHealth;
     public int maxHealth = 4;
 
     private PlayerController player;
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
 
         Scene currentScene = SceneManager.GetActiveScene();
         string sceneName = currentScene.name;
 
         if (sceneName == "StartScene")
         {
             PlayerPrefs.DeleteKey("Health");
             curHealth = 0;
             
             PlayerPrefs.DeleteKey("Score");
             score = 0;
         }
         else
         {
             if (PlayerPrefs.HasKey("Health")) curHealth = PlayerPrefs.GetInt("Health");
             else curHealth = maxHealth;
             if (PlayerPrefs.HasKey("Score")) score = PlayerPrefs.GetInt("Score");
             else score = 0;
         }
     }
 
     private void Update()
     {
         scoreText.text = ("X " + score);
 
         if (curHealth > maxHealth)
         {
             curHealth = maxHealth;
         }
         if (curHealth <= 0)
         {
             Die();
         }
         if (score >= 100)
         {
             GiveHealth();
         }
     }
 
     void GiveHealth()
     {
         score = 0;
         if (curHealth < maxHealth)
         {
             curHealth += 1;
         }
     }
 
     public void TakeDamage(int damage)
     {
         curHealth -= damage;
     }
 
     private void Die()
     {
         SceneManager.LoadScene("StartScene");
     }
 }
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

142 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

Related Questions

Baffling Resources.Load by playerprefs string problem. 1 Answer

Saving highscore with PlayerPrefs 1 Answer

Load Script on Game Start 1 Answer

Initialising List array for use in a custom Editor 1 Answer

PlayerPreffs not saving between plays? 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