I have no expirience with scripting and started a few weeks ago with Unity and some tutorials.
But now something doesnt work, so in my game, when you collide with a coin you get some score.
This is managed by the ScoreManager Script. I made a Playerpref of the ScoreValue and loaded it in the second script in the game over scene. But the value is everytime 11. I cant find the fault. Thank you in advance.
This is the ScoreManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
public TextMeshProUGUI text;
public int score;
// Start is called before the first frame update
void Start()
{
if(instance == null)
{
instance = this;
}
}
public void ChangeScore(int coinValue)
{
score += coinValue;
text.text = "Score :" + score.ToString();
}
public void SaveData()
{
PlayerPrefs.SetInt("ScoreText", score);
PlayerPrefs.Save ();
}
}
and this is the Loadscript in the GameOver scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class LoadData : MonoBehaviour
{
private int score;
public TextMeshProUGUI text;
// Start is called before the first frame update
void Update()
{
score = PlayerPrefs.GetInt("ScoreText");
text.text = "Score :" + score.ToString();
}
}