2

This is how I save leveldata in my development kit.(this runs in dev program)
And the data is able to be correctly restored in dev kit aswell.

public void Savedata()
{
    List<List<float>> tempfloatlist = new List<List<float>>();

    foreach (List<Vector2> ele in routes)
    {
        conversions.Vec2float temp = new conversions.Vec2float();
        tempfloatlist.Add(temp.conv2float(ele));
    }

    BinaryFormatter binform = new BinaryFormatter();
    FileStream savefile = File.Create(Application.persistentDataPath + 
    "/DevData.bytes");

    DevData savecontainer = new DevData();
    savecontainer.routenames = routenames;
    savecontainer.routes = tempfloatlist;
    savecontainer.waves = waves;

    binform.Serialize(savefile, savecontainer);
    savefile.Close();
}

This is how I try to open the data after I moved the file in resources.(this runs in actual game) See line \ERROR

NullReferenceException: Object reference not set to an instance of an object GameControl.LoadLevelData () (at Assets/GameControl.cs:70) GameControl.Awake () (at Assets/GameControl.cs:26)

I fear that I am not opening the file correct way.

private void LoadLevelData()
{

    TextAsset devdataraw = Resources.Load("DevData") as TextAsset;
    BinaryFormatter binform = new BinaryFormatter();
    Stream loadfile = new MemoryStream(devdataraw.bytes);
    DevData devdata = binform.Deserialize(loadfile) as DevData;
    \\ERROR happens here, no correct data to be loaded in routenames.        
    routenames = devdata.routenames;
    waves = devdata.waves;
    routes = new List<List<Vector2>>();
    foreach (List<float> ele in devdata.routes)
    {
        conversions.float2vec temp = new conversions.float2vec();
        routes.Add(temp.conv2vec(ele));
    }
    loadfile.Close();
}

 [Serializable()]
 class DevData
 {
   public List<List<float>> routes;
   public List<string> routenames;
   public List<Wave> waves;
 }
namespace WaveStructures
{
[Serializable()]
public class Enemy
{
    public int enemytype;
    public int movementpattern;
    public int maxhealth;
    public int speed;
}
[Serializable()]
public class Spawntimer
{
    public float timer;
    public int amount;       
}

[Serializable()]
public class Wave

{
    public List<Enemy> Enemylist;
    public List<Spawntimer> Enemyspawnsequence;
    public int[] enemypool;
}
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Jeroen De Clercq
  • 403
  • 2
  • 10
  • Post the error and the class you want to serialize too. – Programmer Aug 03 '17 at 06:37
  • I think i added what u asked, I am fairly new to programming so feel free to give criticism, I am happy to learn. – Jeroen De Clercq Aug 03 '17 at 06:46
  • Check if `devdataraw.bytes` is null after loading it. Comment out the code below it at this moment. – Programmer Aug 03 '17 at 06:51
  • if (devdataraw == null) { Debug.Log("devdata is null"); } code runs without triggering null, and i commented all code under it out. – Jeroen De Clercq Aug 03 '17 at 07:00
  • Ok good. Now, Ucomment the commented code. Run it again and double click on the Error from the Editor. It will take you to the code line. Tell me what that line of code is. – Programmer Aug 03 '17 at 07:02
  • routenames = devdata.routenames; folowed by another error; patternsource = PathwayConversion.worldpointlist(gamecontrol.routes); The last error is already a different class asking info that is supposed to be loaded from what i understand. – Jeroen De Clercq Aug 03 '17 at 07:06
  • Could it be that because i save with filestream, and open it with regular stream that a problem occurs there? – Jeroen De Clercq Aug 03 '17 at 07:16
  • I don't know but it's having a hard time deserializing the data. Please post your `Wave` class too. – Programmer Aug 03 '17 at 07:19
  • I added the classes ;The data gets loaded and edited in development program and saves and loads fine. i need to get that data in my resources folder. so i save it as seen above and move the file to resources and open it with the only difference i have; Moved it, and unity whats open it as textasset. – Jeroen De Clercq Aug 03 '17 at 07:26
  • You have an answer and I think you should try that. – Programmer Aug 03 '17 at 07:34

1 Answers1

2

The serializer is having hard time serializing the data.

There are just two possible solution to try:

1.Notice the () in your [Serializable()]. Remove that. That should be [Serializable]. Another user mention that this is valid. Make sure to do #2.

2.Make sure that every class you want to serialize is placed in its own file. Make sure that it does not inherit from MonoBehaviour either.

For example, DevData class should be in its own file called DevData.cs. you should also do this for the Wave and other classes that you will serialize.


Finally, if this does not solve your problem, it's a known problem that BinaryFormatter causes so many issues when used in Unity. You should abandon it and use Json instead. Take a look at this post which describes how to use Json for this.

PassetCronUs
  • 447
  • 4
  • 11
  • Thank you for your help and patience Sir, I will look into it now.Have a nice day. – Jeroen De Clercq Aug 03 '17 at 07:34
  • 2
    Point 1 is a non-issue. `Serializable()` is entirely valid. – Jon Skeet Aug 03 '17 at 07:39
  • Thanks for the clarification. It's just that I haven't seen that used in Unity before. – PassetCronUs Aug 03 '17 at 07:41
  • i am reorganizing my classes a bit first and then ill run a test with a fresh data file from devprogram. ill post if it worked. then i will a look at what Json is and why i should use it. only started learning code a month from scratch and loving it. – Jeroen De Clercq Aug 03 '17 at 08:13
  • 2
    @PassetCronUs I folowed the instructions and the file reads fine now, Thank you. I will now read up on Json if it is better to use. – Jeroen De Clercq Aug 03 '17 at 08:24
  • 1
    Don't forget to [accept](https://meta.stackexchange.com/a/5235) this answer if PassetCronUs's answer worked for you. – Programmer Aug 03 '17 at 08:27