Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

playerprefs spawning errors.

$
0
0
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][1] [1]: /storage/temp/120273-unity-error.png 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); } }

Viewing all articles
Browse latest Browse all 1333

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>