Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gigos22 · Sep 03, 2020 at 11:36 PM · genericsbinarycollections

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges