- Home /
I want to serialize an array of custom classes. Is it possible?
Hi. I've read it should be possible however it gives me an error: NullReferenceException: Object reference not set to an instance of an object GameSaver.SaveGame (System.String fileName) (at Assets/GameSaver.cs:39) PlayerController.Update () (at Assets/Scripts/PlayerController.cs:184)
I tried debugging and i can give values to the SaveData class, problem occurs within the array SavedSun[].
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameSaver : MonoBehaviour {
private GameObject gameController, timeController, playerController;
void Awake () {
DontDestroyOnLoad(gameObject);
}
public void SaveGame(string fileName){ //get controllers here
BinaryFormatter ftr = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/" + fileName + ".dat", FileMode.Open);
gameController = GameObject.FindWithTag ("MainController");
playerController = GameObject.Find ("PlayerController");
timeController = GameObject.Find ("TimeController");
SaveData sd = new SaveData();
//data operations here:
GameObject[] suns = gameController.GetComponent<GameController>().arrayOfSuns;
sd.randomSeed = gameController.GetComponent<GameController>().randomSeed;
sd.spaceSize = gameController.GetComponent<GameController>().spaceSize;
sd.numOfPlayers = gameController.GetComponent<GameController>().numOfPlayers;
sd.year = timeController.GetComponent<TimeController>().year;
sd.month = timeController.GetComponent<TimeController>().month;
sd.day = timeController.GetComponent<TimeController>().day;
sd.allSuns = new SavedSun[gameController.GetComponent<GameController>().getStarCount()];
for (int i = 0; i < sd.allSuns.Length; i++){
sd.allSuns[i].title = suns[i].GetComponent<OwnerProperties>().getName();
sd.allSuns[i].owner = suns[i].GetComponent<OwnerProperties>().getOwner();
sd.allSuns[i].pos = suns[i].transform.position;
sd.allSuns[i].planets = suns[i].GetComponent<SolarSystem>().getNumPlanets();
for (int k = 0; k < sd.allSuns[i].planets; k++){
GameObject thisPlanet = suns[i].GetComponent<SolarSystem>().getPlanet(k); // initialize planets array
sd.allSuns[i].allPlanets[k].climate = thisPlanet.GetComponent<Planet>().getClimate();
sd.allSuns[i].allPlanets[k].seed = thisPlanet.GetComponent<Planet>().planetSeed;
sd.allSuns[i].allPlanets[k].colonized = thisPlanet.GetComponent<Planet>().colonized;
sd.allSuns[i].allPlanets[k].planetMaterial = thisPlanet.GetComponent<Planet>().planetMaterial;
sd.allSuns[i].allPlanets[k].resourceValues = thisPlanet.GetComponent<Planet>().resourceValues;
sd.allSuns[i].allPlanets[k].bIndexes = thisPlanet.GetComponent<BuildingHandler>().bIndexes;
sd.allSuns[i].allPlanets[k].bPositions = thisPlanet.GetComponent<BuildingHandler>().bPositions;
}
}
//---------------------
ftr.Serialize(file, sd);
file.Close();
}
}
[Serializable]
class SaveData {
//player infos
//fleets, generated objects
public int randomSeed;
public int spaceSize;
public int numOfPlayers;
public int year;
public int month;
public int day;
public SavedSun[] allSuns;
}
[Serializable]
class SavedSun {
public string title;
public Vector3 pos;
public int planets;
public int owner;
public SavedPlanet[] allPlanets;
}
[Serializable]
class SavedPlanet {
public string climate;
public int seed;
public bool colonized;
public Material planetMaterial;
public int[] resourceValues;
public List<int> bIndexes; //save bSets, OR recreate bSets from positions!
public List<Vector3> bPositions;
}
Answer by siaran · Mar 15, 2015 at 08:07 PM
Looking at your code, I think the array you get hereGameObject[] suns = gameController.GetComponent().arrayOfSuns;
is empty for some reason. Your error is on line 39, which is this one:
sd.allSuns[i].title = suns[i].GetComponent<OwnerProperties>().getName();
And is also the first time you use your suns array since you declared it. suns[i] is probably not set to anything.
That helps thanks. The suns[] is copied from another script. $$anonymous$$aybe it doesn't work without a constructor i'll have to check that.
Your answer

Follow this Question
Related Questions
Keeping object between edit and play modes. 1 Answer
Cutom Array Inspector in a Custom Inspector 1 Answer
Saving array data? 2 Answers
Does XML Serialization Support "Class Object" Arrays? 1 Answer