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.
var tile : Transform;
var width : int = 100;
var height : int = 100;
static var creat = false;
static var terrainwidth : int;
static var terrainheight : int;
private var tileParent : Transform;
var boolmatrixcreation : Array;
function Start () {
tileParent=GameObject.Find("TileArray").transform;
GenerateTiles();
GenerateHashMap();
Persistent.boolmatrix = boolmatrixcreation;
Application.LoadLevel("Fase 1");
}
function GenerateHashMap(){
boolmatrixcreation = new Array(width);
for(var i=0;i<boolmatrixcreation.length;i++){
boolmatrixcreation[i]=new Array(height);
}
for(i=0;i<boolmatrixcreation.length;i++){
for(var j=0;j<boolmatrixcreation[i].length;j++){
boolmatrixcreation[i][j]=0;
}
}
}
function GenerateTiles(){
// Set Tiles
for(var x = 0; x < width ; x++){
for(var z = 0; z < height; z++){
//Debug.Log("valor de x "+x+" i z "+z);
var tiler = Instantiate(tile,Vector3(x,-1,z),Quaternion.identity);
tiler.parent = tileParent;
tiler.name = "Tile ("+x+","+z+")";
}
}
terrainwidth = width;
terrainheight = height;
}
the GameObject TileArray is where i keep the data array. Has a script named persistent that has the DontDestroyOnLoad.
Hope this helps.