Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Data loss between scenes

Discussion in 'Scripting' started by Wiggin, Sep 2, 2012.

  1. Wiggin

    Wiggin

    Joined:
    Sep 2, 2012
    Posts:
    3
    Hello everyone, I'm developing a game in Unity and it's almost done, if it wasn't for the problem I've been struggling with.
    My game is cyclical, and I need to preserver the data between scenes ( phases of the game ). This part works fine until I do the first
    cycle. When I go from scene 4 (the last phase) to the beginning, the changes in this scene won't preserve.
    It's odd, because the preserving data between scenes 2 to 4 is good, but when I go from 4 to 2 nothing is saved.
    I have a few scenes, which I will describe in detail.

    Scene 0 :Menu: Main menu
    Scene 1: Loading scene
    Loads a grid (not the type grid, an array of plane cubes), and a logical array of ints to keep the changes in the game.
    Scene 2: Phase 1: Place cubes in the scene.
    I generate a random form and the user clicks where he wants to put the form. I check if he can place it, and if he can i mark the logical array positions with the value 3.
    Scene 3 : Phase 2: Place defenses
    I generate a turret that shoots everything in its range (a collider). I place it wherever i want.
    Scene 4 : Phase 3 : Battle
    I generate bots that go towards the random forms, and when these are in range, the bots shoot. Every bullet that impacts with a random form substracts a point in the position where it stands, and if that position is 0, i destroy the form.
    Cycle 1: Back to Scene 2
    If enoughs random forms survive the battle phase, i can place more random forms. Problem is that the logical array didn't sync with the data generated in the battle phase, and some forms that were destroyed in the battle phase in the logical array are marked as if they're still there.

    I use the DontDestroyOnLoad method to preserve the data between scene loads, and all seems to work fine until I go back to scene 2.
    Any suggestions on how to solve that?

    Any feedback is appreciated

    Thanks in advance for your responses.


    Edit
    I'll post some code so you can see the transitions.

    Code (csharp):
    1. var tile : Transform;
    2. var width : int = 100;
    3. var height : int = 100;
    4. static var creat = false;
    5. static var terrainwidth : int;
    6. static var terrainheight : int;
    7. private var tileParent : Transform;
    8. var boolmatrixcreation : Array;
    9.  
    10. function Start () {
    11.     tileParent=GameObject.Find("TileArray").transform;
    12.     GenerateTiles();   
    13.     GenerateHashMap();
    14.     Persistent.boolmatrix = boolmatrixcreation;
    15.     Application.LoadLevel("Fase 1");   
    16.  
    17. }
    18.  
    19. function GenerateHashMap(){
    20.  
    21.     boolmatrixcreation = new Array(width);
    22.     for(var i=0;i<boolmatrixcreation.length;i++){
    23.         boolmatrixcreation[i]=new Array(height);
    24.     }
    25.     for(i=0;i<boolmatrixcreation.length;i++){
    26.         for(var j=0;j<boolmatrixcreation[i].length;j++){
    27.             boolmatrixcreation[i][j]=0;
    28.         }
    29.     }      
    30. }
    31.  
    32.  
    33. function GenerateTiles(){
    34.     // Set Tiles
    35.     for(var x = 0; x < width ; x++){
    36.         for(var z = 0; z < height; z++){
    37.             //Debug.Log("valor de x "+x+" i z "+z);
    38.             var tiler = Instantiate(tile,Vector3(x,-1,z),Quaternion.identity);
    39.             tiler.parent = tileParent;
    40.             tiler.name = "Tile ("+x+","+z+")";
    41.         }
    42.     }
    43.     terrainwidth = width;
    44.     terrainheight = height;
    45.  
    46.  
    47. }
    the GameObject TileArray is where i keep the data array. Has a script named persistent that has the DontDestroyOnLoad.

    Hope this helps.
     
    Last edited: Sep 3, 2012
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Hard to say without some key script points, buuuut perhaps are you initializing a data array in scene 2?

    Can you determine what data makes it back to scene 2? Maybe scene 2's data isn't DontDestroyOnLoad-ed propertly - if you place objects, then go through the cycle, are they all back to how you placed them?
     
  3. Wiggin

    Wiggin

    Joined:
    Sep 2, 2012
    Posts:
    3
    Thanks for the reply.

    The data array is initialized in Scene 1, and then changed in scenes 2 to 4.Since scene 1 is only called once, I don't think that the data is replicated.
    I could make a debug of the array data at each phase start, but the objects seem preserve fine.
    The objects aren't back when I cycle, They're all as they were when the battle phase ended.
    I was wondering if the data values preserve also from scene to scene.
    I'll post some code from the scenes.
     
  4. Wiggin

    Wiggin

    Joined:
    Sep 2, 2012
    Posts:
    3
    Solved. The data was preserving correctly, I was reading it bad. Thanks Loius for the reply and sorry for the inconveniences.