- Home /
All items of any type collection are getting overwritten.
Hi, I save an array of objects: PlayerData[], to a binary file. I am loading the data successfully, but there is one "tiny" problem:
When I try to put each of the objects in another type of collection, all of the objects in the collection becomes the last item the I put. I am pretty certain there is no real problem with the code, but rather a lack of understanding of how things work, but there also might be a mistake in the code..
PlayerData class:
[System.Serializable]
public struct PlayerData
{
public string fullname;
public string id;
public string phone;
public string dateplayed;
public int totalscore;
public int commscore;
public int probscore;
public int deperscore;
public int listenscore;
public PlayerData(PlayerData player)
{
this = player;
}
public PlayerData(string name,string id2,string phone2,string date,int totalscore2,int commscore2,int probscore2,int deperscore2,int listenscore2)
{
fullname = name;
id = id2;
phone = phone2;
dateplayed = date;
totalscore = totalscore2;
commscore = commscore2;
probscore = probscore2;
deperscore = deperscore2;
listenscore = listenscore2;
}
}
AdminManager class: (Where I try to use the de-serialized data )
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AdminManager : MonoBehaviour
{
private List<PlayerData> players;
List<Image> rowsArray = new List<Image>();
public GameObject Parent; // A UI Object - Panel.
public Image Row1Prefab;
public Image Row2Prefab;
bool isSwitch = false;
bool instatiated = false;
private void Start()
{
// Thought that converting it to a List<T> would solve something but I know it is not related.
players = SaveSystem.LoadPlayers().OfType<PlayerData>().ToList();
FillTable();
}
// for debugging only.
void PrintAll()
{
foreach (Image image in rowsArray)
{
Debug.Log(image.transform.GetChild(0).GetComponent<Text>().text);
}
}
private void Update()
{
if (instatiated == false)
{
InstantiatePage();
instatiated = true;
}
}
public void FillTable()
{
for (int i = 0; i < players.Count; i++)
{
// this is where is gets overwritten again and again.
rowsArray.Add(CreateRow(players[i], (isSwitch ? Row1Prefab : Row2Prefab)));
}
}
Image CreateRow(PlayerData player, Image prefab)
{
string[] temp = new string[] { player.fullname, player.id, player.phone, player.totalscore.ToString(), player.dateplayed };
int[] temp2 = new int[] { player.commscore, player.probscore, player.deperscore, player.listenscore };
for (int i = 0; i < prefab.transform.childCount; i++)
{
Transform currentItem = prefab.transform.GetChild(i);
// setting row values.
if (currentItem.GetComponent<Text>() != null)
{
currentItem.GetComponent<Text>().text = temp[i];
}
// setting player info values.
// A UI Image object with Text objects as children.
if (currentItem.GetComponent<Image>() != null)
{
for (int j = 0; j < currentItem.childCount; j++)
{
Transform infoItem = currentItem.GetChild(j);
if (infoItem.GetComponent<Text>() != null)
{
infoItem.GetComponent<Text>().text = temp2[j].ToString();
}
}
}
}
return prefab;
}
void InstantiatePage()
{
foreach (Image image in rowsArray)
{
Instantiate(image, Parent.transform);
}
}
void DestroyRows()
{
for (int i = 0; i < Parent.transform.childCount; i++)
{
Transform current = Parent.transform.GetChild(i);
if (current.GetComponent<Image>() != null && current.CompareTag("Row"))
{
Destroy(current.gameObject);
}
}
}
public void ReturnToMenu()
{
SceneManager.LoadScene("MainMenu");
}
}
SaveSystem class: (in-case you think it's important, although it's pretty basic functionality)
using System.IO;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
using System;
public static class SaveSystem
{
static readonly string path = Application.persistentDataPath + "/players.fun";
public static void SavePlayer(PlayerData player)
{
PlayerData[] data = new PlayerData[] { player };
BinaryFormatter formatter = new BinaryFormatter();
if (File.Exists(path))
{
PlayerData[] data2 = LoadPlayers();
data = data.Concat(data2);
}
FileStream stream = new FileStream(path, FileMode.Create);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData[] LoadPlayers()
{
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData[] data = formatter.Deserialize(stream) as PlayerData[];
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in: " + path);
return null;
}
}
// Using this extension method just to make life easier...
public static T[] Concat<T>(this T[] x, T[] y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
int oldLen = x.Length;
Array.Resize<T>(ref x, x.Length + y.Length);
Array.Copy(y, 0, x, oldLen, y.Length);
return x;
}
}
I appreciate any efforts.
Your answer
Follow this Question
Related Questions
System.Collections.ObjectModel.Collection does not contain a definition for "Items"? 1 Answer
How to create a generic list from a group of gameObjects with specific tag? 1 Answer
I need a good data structure collection for Unity, where can I find it? 2 Answers
Creating a custom List or Collection 1 Answer
Int to binary in unityscript 2 Answers