- Home /
figured it out
variables not resetting when reloading scene
When I reload my scene using SceneManager.LoadScene() the camera size & a couple other variables dependent upon screen size are totally different than when I load the scene the first time. What changes is the camera size, the Y position of my foreground (running path), & the point on the X-axis when objects are destroyed off screen. The first time the scene loads everything runs smooth, but as soon as i reload the scene these 3 get out of whack. Because all 3 reference the camera and/or the screen size I imagine that's where the root issue may lie.
When starting the scene the first time my camera size gets set to 80. When i reload the scene it goes down to 5. For testing other aspects i set the camera size to 80 in update on this script, but i assume this will break as soon as i test on different res devices. Here's my script for determining the camera size:
public static float pixelsToUnits = 1f;
public static float scale = 1f;
public Vector2 nativeResolution = new Vector2(240, 160);
void Awake () {
var camera = GetComponent<Camera>();
if (camera.orthographic){
scale = Screen.height/nativeResolution.y;
pixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / pixelsToUnits;
}
}
void Update(){
var camera = GetComponent<Camera>();
//change camera size to size it sets when starting game;
camera.orthographicSize = 80f;
}
The position of the floor/foreground is set in my game manager script. The y position is usually around -73, but varies slightly depending on the exact size for that scene. Regardless, when i reload it places itself right in the middle, roughly @ -0.3 on the y-axis:
public GameObject playerPrefab;
public Text continueText;
public Text scoreText;
private float timeElapsed = 0f;
public static float bestTime = 0f; //make private non-static to reset
private float blinkTime = 0f;
private bool blink;
public static bool gameStarted;
public static bool gameOver;
private timeManager TimeManager;
private GameObject player;
private static GameObject floor; //make private nonstatic to reset
private Spawner spawner;
private bool beatBestTime;
//AudioSource audio;
static float floorSize;
void Awake(){
floor = GameObject.Find("Foreground");
spawner = GameObject.Find("Spawner").GetComponent<Spawner>();
TimeManager = GetComponent<timeManager>();
DontDestroyOnLoad(playerPrefab);
}
// Use this for initialization
void Start () {
var floorHeight = floor.transform.localScale.y;
var pos = floor.transform.position;
pos.x = 0;
pos.y = -((Screen.height / pixelPerfectCamera.pixelsToUnits) / 2) + (floorHeight / 2);
//floor.transform.position = pos;
spawner.active = false;
Time.timeScale = 0;
continueText.text = "Press any button to begin";
bestTime = PlayerPrefs.GetFloat("BestTime");
gameOver = false;
}
Lastly the script I'm using to destroy the objects when they go off screen. Again, this works great the first time i load the scene, but each subsequent time, the x value for where objects are destroyed gets closer & closer to zero:
public float offset = 16f;
public delegate void OnDestroy();
public event OnDestroy DestroyCallback;
private bool offscreen;
public static float offscreenX = 0f; // make private non static to reset
private Rigidbody2D body2d;
void Awake(){
Debug.Log("initial offscreen value: " + offscreenX);
body2d = GetComponent<Rigidbody2D>();
//offscreenX = (Screen.width/pixelPerfectCamera.pixelsToUnits) / 2 + offset; // remove to reset
Debug.Log("initial offscreenX: " + offscreenX);
}
// Use this for initialization
void Start () {
offscreenX = (Screen.width/pixelPerfectCamera.pixelsToUnits) / 2 + offset;
Debug.Log("offscreenX is: " + offscreenX);
Debug.Log("screen width: " + Screen.width);
}
// Update is called once per frame
void Update () {
var posX = transform.position.x;
var dirX = body2d.velocity.x;
if(Mathf.Abs(posX) > offscreenX){
if(dirX < 0 && posX < -offscreenX){
offscreen = true;
} else if(dirX > 0 && posX > offscreenX){
offscreen = true;
}
} else{
offscreen = false;
}
if(offscreen){
GetComponent<SpriteRenderer>().enabled = true; //remove this to reset
onOutOfBounds();
}
}
public void onOutOfBounds(){
offscreen = false;
gameObjectUtility.Destroy(gameObject);
if(DestroyCallback != null){
DestroyCallback();
}
}
I know these are 3 different issues, but they all seem related. I'm hoping someone can spot something I'm missing or shed some light on concepts I've overlooked. Any & all help is greatly appreciated.
Answer by grantdesselle · Jun 03, 2017 at 08:07 PM
I finally figured it out. In case anyone is having a similar issue. I needed to reset the scale value after the player died. If not, it remains at 6.25 & causes the other downstream events. I'm embarrassed i didn't see it before, knowing full well that static values aren't reset when reloading scenes it should have been obvious.
Thanks a lot for sharing your answer. I was facing the similar issue.
Follow this Question
Related Questions
Adding GameObject to selected Scene 1 Answer
How can i transfer an object from scene1 and instantiate it in scene2? 2 Answers
Variables are resetting after loading the play scene 1 Answer
unity photon doesn't sync scene for master 0 Answers
Make the animation play at the start of the time waited, not the end of it? 1 Answer