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 BlazerLazer · Jul 06, 2018 at 10:33 PM · scripting problemerrorplayerprefs

playerprefs spawning errors.

In my game I use playerprefs to keep track of what level ive reached, but its impossible to run because the same error is spawned every frame. It looks like this:alt text

I have no clue what this means or how to fix it. If anybody can shed some light on this then it would be a life-saver. I will also put the script here just in case you see an actual error.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class body : MonoBehaviour {
 
     public Text WinText;
     public Text ScoreText;
     public Image Shield1;
     public Image Shield2;
     public AudioSource CoinSound;
    public int Score = PlayerPrefs.GetInt("Score", 0);
     public int Scene;
     public int LevelToUnlock;
     public int LevelReached = PlayerPrefs.GetInt("levelReached", 1);
     public int Health = PlayerPrefs.GetInt("Health", 1);
     public Button[] levelButtons;
     public Text[] ButtonTexts;
     public int NextLevel;
 
      //Use this for initialization
     void Start () {
 
         LevelReached = PlayerPrefs.GetInt("levelReached");
 
         Debug.Log("playerPref says " + PlayerPrefs.GetInt("levelReached"));
         Debug.Log("editor says " + LevelReached);
 
         Debug.Log("1");
 
        for (int i = 0; i < levelButtons.Length; i++)
         {
             Debug.Log("2");
 
             LevelReached = PlayerPrefs.GetInt("levelReached");
 
             if (i + 1 > LevelReached)
            {
                 Debug.Log("3");
 
                 levelButtons[i].enabled = false;
                 ButtonTexts[i].enabled = false;
 
                 Debug.Log("4");
            }
         }
 
         Health = PlayerPrefs.GetInt("Health", 1);
 
         if (Health < 2)
         {
             Shield1.enabled = false;
         }
        if (Health < 3)
         {
             Shield2.enabled = false;
         }
 
         LevelReached = PlayerPrefs.GetInt("levelReached");
 
         for (int i = 0; i > levelButtons.Length; i++)
         {
             LevelReached = PlayerPrefs.GetInt("levelReached");
 
             if (i + 1 > LevelReached)
             {
                 levelButtons[i].enabled = false;
                 ButtonTexts[i].enabled = false;
             }
         }
 
     }
     
     // Update is called once per frame
     void Update () {
 
         Score = PlayerPrefs.GetInt("Score");
 
         ScoreText.text = "Score: " + Score;
         if (Health < 2)
         {
             Shield1.enabled = false;
         }
         if (Health < 3)
         {
             Shield2.enabled = false;
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag("Enemy"))
         {
             PlayerPrefs.SetInt("Health", PlayerPrefs.GetInt("Health") - 1);
             Health = PlayerPrefs.GetInt("Health");
     
             if (Health == 0)
             {
                 PlayerPrefs.SetInt("Health", 1);
                Health = PlayerPrefs.GetInt("Health");
                 SceneManager.LoadScene(Scene);
             }
         }
 
         if (other.CompareTag("Finish"))
         {
             StartCoroutine(Finish());
         }
 
         if (other.CompareTag("Coin"))
         {
             CoinSound.Play();
             PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score") + 10);
        }
     }
 
    IEnumerator Finish()
     {
         Debug.Log("Level Won!");
 
         WinText.enabled = true;
 
         if (LevelReached < LevelToUnlock)
         {
             PlayerPrefs.SetInt("levelReached", PlayerPrefs.GetInt("levelReached") + 1);
         }
     
        yield return new WaitForSeconds(3f);
 
         SceneManager.LoadScene(NextLevel);
    }
 }
 

unity-error.png (8.0 kB)
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
1
Best Answer

Answer by hexagonius · Jul 07, 2018 at 07:02 AM

line 17: public int Score = PlayerPrefs.GetInt("Score", 0);

that's what you cannot do. do the assignment in awake instead.

 public int Score;
 
 private void Awake()
 {
      Score = PlayerPrefs.GetInt("Score", 0);
 }

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 BlazerLazer · Jul 08, 2018 at 07:54 PM 0
Share

thank you. this saved my game.

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

173 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

Related Questions

Where is the problem with the script, i want my player to die when his health is <1 1 Answer

No Monobehaviour scripts in files 1 Answer

Why does Unity crash with this code ? 2 Answers

UNetWeaver error: Exception 0 Answers

error CS0118: `UnityEngine.Object.Destroy(UnityEngine.Object, float)' is a `method' but a `type' was expected problem 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