- Home /
Can't use .Add-Function for List
Hi, I'm struggling with Adding new Elements to a List that get's serialized afterwards. Getting a bit confused here. I have a SaveManager.cs storing a List-Class and the Functions for Saving and loading:
public class SaveManager : MonoBehaviour {
public static SaveManager control;
public class Error
{
public float X; public float Y; public float Z;
public Error( float newX, float newY, float newZ)
{
X = newX; Y = newY; Z = newZ;
}
}
public List<Error> ErrorList = new List<Error>();
[Serializable]
public class ListContainer
{
public List<Error> error;
}
void Awake()
{
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != this)
{
Destroy(gameObject);
}
Load();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/Data.dat");
ListContainer lc = new ListContainer();
lc.error = ErrorList;
bf.Serialize(file, lc.error);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/Data.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/Data.dat", FileMode.Open);
ErrorList = (List<Error>)bf.Deserialize(file);
file.Close();
Debug.Log("SaveFile Found and loadad");
} else
{
Debug.Log("No SaveFile Found. Started Clean");
}
}
}
Then i have a script for adding the entrys to the list. And there is the problem. I can' using the Add-Function:
public List<SaveManager.Error> ErrorList = new List<SaveManager.Error>();
public void Create () {
float newX = ErrorX;
float newY = ErrorY;
float newZ = ErrorZ;
ErrorList.Add(new Error( newX, newY, newZ)); //<- Error: The type or namespace name `Error' could not be found.
}
Any idea why? I guess it's super obvious -.-
Answer by hexagonius · Dec 07, 2018 at 10:29 AM
It's new SaveManager.Error
there too. Error is a nested class of SaveManager.
That's of course the main error at hand, however keep in $$anonymous$$d that your "ErrorList" that you create inside any other class has nothing to do with the "ErrorList" inside your Save$$anonymous$$anager class. You only serialize that list. Just because you declare another variable within another scope with the same name, doesn't mean they belong together.
This seems to be for logging purposes. You usually want to have one instance of your Save$$anonymous$$anager and have other classes use that instance to log / add their errors to the list of that Save$$anonymous$$anager instance. In most cases one would use either a singleton or a static list for this. It looks like you already have a singleton (sort of) but you're not using it?
Also note that your "Error" class isn't marked with the Serializable attribute.
Yes, I am realizing that something is wrong. I went through this tutorial here about persistent Data and also this about Lists/Dictionarys. thought I can adapt it to save a whole class persistent. But I think this isn't the case. Or at least I'm saving it right, but did not add the values right.
What I want is to "fill up" the ErrorList in Save$$anonymous$$anager and save it. That's it. Can I kindly ask you for a solution or helping hand?
Also note that your title and error don't belong to each other. There is no problem with Add. If you break up your line into:
var err = new Error( newX, newY, newZ);
ErrorList.Add(err);
You will notice the error is in the first of those two lines, not in the second. Chaining several things into one line is fine, but if you have trouble interpreting an error you may want to break up that line into several seperate lines if possible which makes it easier to pinpoint the issue.