- Home /
Error while trying to enter data to the array 2nd time.
#pragma strict
import System;
import System.Runtime.Serialization.Formatters.Binary;
import System.IO;
var data:PlayerData;
var LevelValue:int;
var Level:int;
var Coins:int;
var UnlockedTopID:int;
var UnlockedTops:Array;
private var WorldScript:WorldNew;
var LevelText:UnityEngine.UI.Text;
var LevelMeter:UnityEngine.UI.Slider;
var CoinCounter:UnityEngine.UI.Text;
function Save()
{
var bf:BinaryFormatter=new BinaryFormatter();
var file:FileStream=File.Create(Application.persistentDataPath +"/playerInfo.dat");
data.LevelValue=LevelMeter.value;
data.Level=WorldScript.Level;
data.Coins=int.Parse(CoinCounter.text);
bf.Serialize(file,data);
file.Close();
}
function UnlockedTopSave(id:int)
{
////This function is called when ever a purchase of an item in inventory is done with an id ranging from 1 to 10
if(id!=0)
{
UnlockedTopID=id;
UnlockedTops.Add(id);
for(var j:int=0;j<UnlockedTops.length;j++)
{
data.UnlockedTops.Add(UnlockedTops[j]);
}
Debug.Log("Length of unlocked tops"+UnlockedTops.length);
Save();
}
}
function Load()
{
if(File.Exists(Application.persistentDataPath +"/playerInfo.dat"))
{
var bf:BinaryFormatter=new BinaryFormatter();
var file:FileStream=File.Open(Application.persistentDataPath +"/playerInfo.dat",FileMode.Open);
var data:PlayerData=bf.Deserialize(file) as PlayerData;
LevelValue=data.LevelValue;
Level=data.Level;
Coins=data.Coins;
UnlockedTops=data.UnlockedTops;
file.Close();
}
}
function Start ()
{
WorldScript=this.GetComponent(WorldNew);
UnlockedTops=new Array();
data=new PlayerData();
}
class PlayerData extends System.Object
{
var LevelValue:int;
var Level:int;
var Coins:int;
var UnlockedTops:Array=new Array();
}
This is my code of saving and loading data in my game. While I try to save a list of items using an array and their integer id, if I purchase the first item, it enters correctly to the array. THen if I quit the game play from the unity editor, and then plays again, the data is loaded correctly to the array, but when I again purchase a second thing , the 1st purchased data is overwritten. I need to save both the data on the second item purchase. Please help me to find the error in my code. Please do help me. Thanks in advance..
After you load the data, find the number of elements in your array, Array.length or List.count and use that figure to append to the end of your collection.
If your PlayerData is meant to emulate a struct you need to extend System.ValueType
You need to utilise a constructor for this to work properly.
Here's a script I use. Notice the function matches the class. You need to call this function when creating a new datablock.
#pragma strict
public class BlockStruct extends System.ValueType
{
var blockID : int;
var blockType : int;
var blockName : String;
var blockPosition : int;
var positionsWhenSpawned : int;
var thisTransform : Transform;
public function BlockStruct( blockID : int,
blockType : int,
blockName : String,
blockPosition : int,
positionsWhenSpawned : int,
thisTransform : Transform
)
{
this.blockID = blockID;
this.blockType = blockType;
this.blockName = blockName;
this.blockPosition = blockPosition;
this.positionsWhenSpawned = positionsWhenSpawned;
this.thisTransform = thisTransform;
}
}
To create a 'BlockStruct'
private var paddingZero : BlockStruct = BlockStruct( 0, 0, "Padding", 0, 0, nullTransform); //This syntax look familiar? :)
So, make your PlayerData look like that and use the constructor function to create new data and you should find that your data is no longer defaulting, one would hope.
Your answer