Help! my list won't remove its elements...
So, the story is, I am trying to create a collection system for my RPG that, e.g. once it first reaches a scene, it will "unlock" a picture, and I tried to do this by setting up a list and a function that once it runs, it will first add a string element (which is the name of a game object) to the list, then save it locally. And I plan to extract info from the string by going through the string elements and show the game objects with names that are equivalent to the strings (so find game object with name string).
So as a coding beginner, I wrote the code for adding and saving string, as below:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System; public class SCP : MonoBehaviour { public static SCP controlClt;
public List<String> meCltName = new List<String>();//I should put this elsewhere that would only run in the beginning of the game?
private void Awake()
{
if (controlClt == null)
{
DontDestroyOnLoad(gameObject);
controlClt = this;
}
else if (controlClt != this)
{
Destroy(gameObject);
}
if (File.Exists(Application.persistentDataPath + "/collectionInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/collectionInfo.dat", FileMode.Open);
CP data = (CP)bf.Deserialize(file);
file.Position = 0;
file.Close();
meCltName = data.meCltName;//the list is saved into data---this is just modification from the previous save tutorial code
}
}
public void addtoCltName(GameObject me)//this function will add a game object input to collection and also save it in folder
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/collectionInfo.dat");
CP data = new CP();
String mee = me.name;
meCltName.Add(mee);//add the inputed gameobject name to the list meCollection
data.meCltName = meCltName;
bf.Serialize(file, data);
file.Close();
}
[Serializable]
class CP
{
public List<String> meCltName;
}
But when I tested out weird things happened. At first I didn't really know how it works so when I first dragged the script to a game object I set the list size as 20. Then I created a button and put the script of adding and saving the string onto it. When I clicked on the button I realized that it indeed added a new element, to the end of the list every time I clicks it, and the size increases. However, I want to start over and remove all those 20 empty game objects. However when I tried to resize it to, e.g. 0, after I exited play mode, it will show to be resized but once I entered the game the list returned to how it was last time. I closed the editor and restarted it but the 20+ elements were still there, and once an element is added when I click the button in play mode, it won't go away.
Your answer
Follow this Question
Related Questions
How can I save this objects ? 0 Answers
Saving And Loading List error 1 Answer
Serialize a list to save it 3 Answers
Problem to add on List (Serializable class) 2 Answers
How to get tooltips to pop up on certain levels, but not all of them 2 Answers