Search Unity

Help with variable scope.

Discussion in 'Editor & General Support' started by SuperUltraHyper, Dec 15, 2015.

  1. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    I have a sequence of levels that are loaded one after the other. Each has a prefab Gameobject with the same sctipt attached. It looks like this:
    Code (CSharp):
    1. public class LevelLoader : MonoBehaviour {
    2.  
    3.     public float setupWaitTime = 2.1f;
    4.  
    5.     // Use this for initialization
    6.     void Start() {
    7.        Debug.Log("Set up wait =  " + setupWaitTime  );
    8.        Destroy(gameObject);
    9.     }
    10. }
    In the editor, and in each level, I have the float setupWaitTime = 1.

    For the first level the output is as expected; 1.
    For the second level the value in the editor is 1 but the Console Output shows it at the default 2.1

    For reference, the levels are loaded slowly with > 1m between levels loads.

    Any ideas what I am doing wrong?

    Thanks for the help!!
     
  2. Yowling-Cat-Games

    Yowling-Cat-Games

    Joined:
    Dec 11, 2014
    Posts:
    37
    Would this work for ya..


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelLoad : MonoBehaviour {
    5.  
    6.     public float setupWaitTime = 2.1f;
    7.  
    8.     // Use this for initialization
    9.     void Start() {
    10.         Debug.Log("Set up wait =  " + setupWaitTime  );
    11.         Destroy(this.gameObject);
    12.         Debug.Log("Game Object Destroyed!");
    13.     }
    14. }
    I would also name each Empty gameobject to some thing like levelOneObject levelTwoObject...

    Don't know for sure..if this will work...cause I have no idea how your game is setup... ;)

    Lar
     
  3. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    One thing of note: All loads are done Asynchronously like this: "Application.LoadLevelAsync(levelToLoad);"

    These suggestions didn't make a difference but we are on to somthing maybe.
    The GameObjects are Destroyed as expected.
    At no time do they co exist.
    The Debug.Log "Destroyed!" is executed? Is that correct.

    I Changed the GameObject Names to "Level Loader" and "Level Loader 1111" for testing.
    I changed the code like so:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelLoad : MonoBehaviour {
    4.     public float setupWaitTime = 2.1f;
    5.     public string[] levelsToLoad;
    6.  
    7.     // Use this for initialization
    8.     void Start() {
    9.         Debug.Log("Set up wait =  " + setupWaitTime  );
    10.        Destroy(this.gameObject);
    11.         Debug.Log("Game Object Destroyed = " + this.gameObject.name);
    12.         for (int i = 0; i < levelsToLoad.Length; i++)
    13.             Debug.Log("Vars= " + i + ", "+ levelsToLoad[i] + ", Wait = " + timeToWait);
    14.     }
    15. }
    Why is the last debug line even executing?

    Here is the odd part... The array of level names works as expected but the timeToWait Value isn't.

    For example the output is as follows.
    *Level Load 1*
    Set up wait = 1
    Game Object Destroyed = Level Loader
    Vars = 0, String1, Wait = 1
    Vars = 1, String2, Wait = 1
    *Level Load 2*
    Set up wait = 2.1
    Game Object Destroyed = Level Loader
    Vars = 0, String3, Wait = 2.1
    Vars = 1, String4, Wait = 2.1

    The odd things are:
    The GameObject name from *Level Load 2* isn't "Level Loader 1111"
    The wait time is returning to a default value of 2.1 and not 1.
    The Array of strings is correct in both cases. WTF?
     
  4. Yowling-Cat-Games

    Yowling-Cat-Games

    Joined:
    Dec 11, 2014
    Posts:
    37
    Well....this is what I quickly put together at this end...


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class LevelLoad : MonoBehaviour {
    4.  
    5.     //Public variables ---> able to access through inspector
    6.     public float setupWaitTime = 2.1f;
    7.     public string[] levelsToLoad;
    8.  
    9.  
    10.     void Start()
    11. {
    12.         //Show our initial wait time
    13.         Debug.Log("Set up wait =  " + setupWaitTime);
    14.  
    15.         //Ramble---> thru the array length and load our levels
    16.         for (int i = 0; i < levelsToLoad.Length; i++) {
    17.             //Print / load our levels
    18.             Debug.Log("Vars= " + i + ", "+ levelsToLoad[i] + ", Wait = " + setupWaitTime);
    19.         }
    20.  
    21.         //If all levels are loaded then destroy our level loader object
    22.         Destroy(this.gameObject);
    23.         Debug.Log("Game Object Destroyed = " + this.gameObject.name);
    24.         //All success!
    25.         Debug.Log("All Available Levels Loaded Successful!");
    26.  
    27.        }
    28.  
    29. }
    One(1) 'gameObject' (empty game object) with One(1) script attached...

    after all levels loaded 'gameObject' is Destroyed..

    Results in the 'attachments' below..is this what your after...or steer in the right direction?

    It appears to work, like I understand you...and what you wanted..right?

    NOTE: -- I'm using Unity 5.1.2p2 --

    Lar
     

    Attached Files:

    Last edited: Dec 15, 2015
  5. Yowling-Cat-Games

    Yowling-Cat-Games

    Joined:
    Dec 11, 2014
    Posts:
    37
    Here's another version that may get you thinking also....

    Code (CSharp):
    1. //*** LevelLoad.cs
    2. //*** load number of specfied levels then wait specfied time then load another level.
    3. //***
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7.  
    8. public class LevelLoad : MonoBehaviour {
    9.  
    10.     //Public variables
    11.     public float setupWaitTime = 2.1f;
    12.     public string[] levelsToLoad;
    13.     private float timeLeft;
    14.     private static bool doneLoading = false; // Make static across all levels
    15.     private int index = 0;
    16.  
    17.  
    18.  
    19.     void Update ()
    20.     {
    21.         if (doneLoading == false)
    22.         {
    23.             timeLeft -= Time.deltaTime;
    24.            
    25.             if ( timeLeft < 0.0f )
    26.             {
    27.                 Debug.Log("Time Reset!");
    28.                 timeLeft = setupWaitTime;
    29.                 doneLoading = LoadMyLevel();
    30.             }
    31.        
    32.         }
    33.  
    34.     }
    35.  
    36.     bool LoadMyLevel()
    37.     {
    38.         //Setup our own for loop more or less just another way to count our level loads and time them
    39.         Debug.Log("Level: " + (index + 1) + ", "+ levelsToLoad[index] + ", Wait Time: " + timeLeft);
    40.  
    41.         index++;
    42.  
    43.         if (index == levelsToLoad.Length)
    44.         {
    45.             doneLoading = true;
    46.             //If all levels are loaded then destroy our level loader object
    47.             Destroy(this.gameObject);
    48.             Debug.Log("Game Object Destroyed = " + this.gameObject.name);
    49.             //All success!
    50.             Debug.Log("All Available Levels Loaded Successful!");
    51.         }
    52.         return doneLoading; // Return our progress as to stop timer in Update()
    53.     }
    54.  
    55. }
    Let me know... :)

    Lar
     
  6. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    Turns out I needed the magic [SerializeField] on the vars and it started working.