Hi, I am working on saving and loading gameobjects in my game. I am trying to save the players position and load it up at that particular transform position. I have everything working for objects that do not reference any scripts, but as soon as I try to move my player, I get a ton of errors saying that the player does not exist. I believe it is because of this line of code in particular (in my SaveGameManager script):
public void Load () {
foreach (SaveableObject obj in SaveableObjects)
{
if (obj != null)
{
Destroy(obj.gameObject); //This is what destroys my gameobjects before re-instantiating them on 'load'
}
}
I have tried telling the Load function instead of destroying the gameobjects, to just move them (like this):
transform.localPosition = obj.gameObject.transform.localPosition;
But this gives me an error relating to this like of code in my SaveGameManager (The error reads “NullReferenceException: Object reference not set to an instance of an object”):
if (tmp != null) //if we have not already instantiated the object...
{
tmp.GetComponent<SaveableObject>().LoadData(value); //instantiate the object
}
(“tmp” is an array of gameobjects that are ‘saveable’)
Anyone out there that can help me with this issue? I feel like it is right in front of my eyes - I’m just not seeing it.