- Home /
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:
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
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);
}