When I make whatever modification to whatever script and save during play, the static variables of my GameManager become null, except for the variables that not make reference to a gameObject. so all references to GameManager’s static variables become impossible and give a bunch of errors. I tried to do this in 2020.1 and 2020.2 and both give the same result. I tried to make another project and making a static variable that is set to this in awake, and put on Update Debug.Log(instance == null);
. At the start, prints false, when I add a comment or whatever thing, start printing true.
Here’s the GameManager class:
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public static GameManager instance { get; private set; }
public static Camera mainCamera { get; private set; }
public static Vector2 screenBounds = new Vector2();
public Transform player;
private float lastPlayerY;
private float travelledY;
private void Awake() {
if (instance != null) {
Destroy(this);
return;
} else
instance = this;
mainCamera = Camera.main;
screenBounds.x = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x;
screenBounds.y = mainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height, 0)).y;
}
private void Start() {
travelledY = 0;
lastPlayerY = player.position.y;
}
private void Update() {
if (mainCamera.WorldToScreenPoint(player.position).y < -screenBounds.y)
GameOver();
if (player.position.y > lastPlayerY) {
travelledY += player.position.y - lastPlayerY;
lastPlayerY = player.position.y;
}
}
public void GameOver() {
Debug.Log(GetScoreFromY(travelledY));
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public static int GetScoreFromY(float f) {
return (int) (f * 10);
}
public static float GetYFromScore(int i) {
return (float) i / 10;
}
public int GetScore() {
return GetScoreFromY(travelledY);
}
}
If I replace GameManager.mainCamera with Camera.main… it works so it’s a huge Bug? Thanks for all your support