Not saving items collected next play with binary saving system

Hi guys
I’m new at unity
I have a problem with saving items collected like coins and stars in binary saving system.
How can I save coins collected and not show next play.
Ofcorse I can do it for few number but it’s too much repeating for more item I have 90 stars that after player collected them,they should be disappeard
Please help me

Hi,
Simple way to do it is by creating a list of objects and save that list into your binary file.

public class CoinStarItem
{
   public int Index {get; set;}
   public bool Visible {get; set;}
   public GameObject ActualGameObject {get; set;}
   public Vector3 position {get; set;}
   public TypeOfItem ItemType {get; set;}
}

Then you have to save this list infos into the binary file…
List<CoinStarItem> items;
Actualy you need to save (index, visibility, item type and position)

And at loading, repopulate the list and apply visibility;

Thank u very much
But I dont use set and get for saving
This way : index=database.index ;
How can I save this variable to the list
And how to use list after load

Loading should be something like this:

foreach(var database in databaseFields)
{
  CoinStarItem item = new CoinStarItem();

  TpeOfItem t = database.typeOfItem;
  GameObject go = CreateInstance(t == Coin ? coinPrefab : starPrefab);
  item.Index = database.index;
  item.Visible = database.itemVisible;
  item.position = database.position;
  go.SetActive(item.Visible);
  go.transform.position = item.position;
  item.ActualGameObject = go;

  items.Add(item);
}

Save should be something like:

foreach(var item in items)
{
  //now just populate your database
  database.index = item.Index;
  etc...
  save(database field);
}

Sorry but I can’t understand what should I’d do

Here is my code

using System.Collections;
using System;
using System.IO;
using System.Collections.Generic;

using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class DataManagement : MonoBehaviour
{
    public static DataManagement datamanagement;

 
    public int index;
    public bool visible;
    public GameObject ActualGameobject;
    public Vector3 posision;

    private void Awake()
    {
        if (datamanagement == null)
        {
            DontDestroyOnLoad(gameObject);
            datamanagement = this;
        }
        else if (datamanagement != this)
        {
            Destroy(gameObject);
        }
    }

 
    public void SaveData()
    {
        BinaryFormatter binaryForm = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/gameInfo8.mtr");
        gameData data = new gameData();

        data.index = index;
        data.visible = visible;
        data.ActualGameobject = ActualGameobject;
        data.posision = posision;

        binaryForm.Serialize(file, data);
        file.Close();
    }


    public void LoadData()
    {
        if (File.Exists(Application.persistentDataPath + "/gameInfo8.mtr"))
        {
            BinaryFormatter binaryForm = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/gameInfo8.mtr", FileMode.Open);
            gameData data = (gameData)binaryForm.Deserialize(file);
            file.Close();

          

        }

        else { Debug.Log("No dataBase"); }
    }




    [Serializable]

    class gameData
    {

        public int index;
        public bool visible;
        public GameObject ActualGameobject;
        public Vector3 posision;

    }








}

To save/load: https://wiki.unity3d.com/index.php/Saving_and_Loading_Data:_XmlSerializer

GameData

public enum eTypeOfGameData
{
    NONE = 0,
    COIN,
    STAR
}

public class GameData
{
    public int Index {get; set;}
    public bool Visible {get; set;}
    public Vector3 Position {get; set;}
    public eTypeOfGameData TypeOfGameData {get; set;}
}
//this is coin/star class and you should attach this to your coin/star object.. also is not completed
public class CoinStar
{
    private GameData MyGameData;
    private bool StatusVisibility;
 
    public void SetGameData(GameData gameData)
    {
        MyGameData = gameData;
        StatusVisibility = gameData.Visible;
        transform.position = MyGameData.Position;
    }
 
    public void SetVisibility(bool value)
    {
        MyGameData.Visible = value;
    }
 
    void Update()
    {
        //your code here
    
        if (StatusVisibility != MyGameData.Visible)//visibility changed
        {
            StatusVisibility = MyGameData.Visible;
            if (!MyGameData.Visible)//this is invisible
            {
                GetComponent<Renderer>().enabled = false;
                //gameObject.active = false; if you want to disable the gameObject
            }
            else
            {
                GetComponent<Renderer>().enabled = true;
            }
            SaveToFile();//here you should call your method to save this new stuff into your file
        }
    }
}
//when player interact with your coin/star just call
col.gameObject.GetComponent<CoinStar>().SetVisibility(false);
//Load data should return a bool .. to tell you if everything was ok
//place try.. catch in LoadData
//After LoadData()
public void SetGameData(List<GameData> gameDatas)
{
    foreach(var gd in gameDatas)
    {
        if (gd.TypeOfGameData == eTypeOfGameData.COIN)
        {
            InstantiateObject(coinPrefab, gd);
        }
        else if (gd.TypeOfGameData == eTypeOfGameData.STAR)
        {
            InstantiateObject(starPrefab, gd);
        }
    }
}
public void InstantiateObject(GameObject prefabObject, GameData gd)
{
    //https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    GameObject go = Instantiate(prefabObject);
    CoinStar cs = go.GetComponent<CoinStar>();
    cs.SetGameData(gd);
    //you should always check if go, cs, prefab object is not null, else it will crash when one of those is null
}