I made my first game, it is a rip off of flappy bird and want to make a highscore that needs to be saved over the scenes. Since when my bird dies i reset the scene.
The problem is:
NullReferenceException: Object reference not set to an instance of an object
SharedR.Start () (at Assets/SharedR.cs:12)
So how can i save values over a scene, since i cant make code in another scene to prevent it.
SharedR is in another scene
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SharedR : MonoBehaviour
{
public int Highscore;
public logicscript logic;
void Start() {
logic.playScore = 0;
}
// Update is called once per frame
void Update()
{
if (logic.playScore >= Highscore && logic.playScore > 0)
{
Highscore = logic.playScore;
}
Debug.Log("Highscore: " + Highscore);
}
}
and:
public class logicscript : MonoBehaviour
{
public int playScore = 0;
public void addScore() {
playScore++;
scoreText.text = playScore.ToString();
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}