Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 03, 2017 at 08:09 PM by grantdesselle for the following reason:

figured it out

avatar image
0
Question by grantdesselle · Jun 07, 2017 at 07:06 AM · scene-switchingreloadscreen sizes

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omigator · Jun 05, 2019 at 07:28 PM 0
Share

Thanks a lot for sharing your answer. I was facing the similar issue.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges