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_.title = suns*.GetComponent<OwnerProperties>().getName();*_
sd.allSuns_.owner = suns*.GetComponent().getOwner();
sd.allSuns.pos = suns.transform.position;
sd.allSuns.planets = suns.GetComponent().getNumPlanets();
for (int k = 0; k < sd.allSuns.planets; k++){
GameObject thisPlanet = suns.GetComponent().getPlanet(k); // initialize planets array*
sd.allSuns*.allPlanets[k].climate = thisPlanet.GetComponent().getClimate();
sd.allSuns.allPlanets[k].seed = thisPlanet.GetComponent().planetSeed;
sd.allSuns.allPlanets[k].colonized = thisPlanet.GetComponent().colonized;
sd.allSuns.allPlanets[k].planetMaterial = thisPlanet.GetComponent().planetMaterial;
sd.allSuns.allPlanets[k].resourceValues = thisPlanet.GetComponent().resourceValues;
sd.allSuns.allPlanets[k].bIndexes = thisPlanet.GetComponent().bIndexes;
sd.allSuns.allPlanets[k].bPositions = thisPlanet.GetComponent().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 bIndexes; //save bSets, OR recreate bSets from positions!*
* public List bPositions;*
}