Creating an Automatic Save Structure

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Re: Creating an Automatic Save Structure

Post by Joel »

Hi there,

In the line you quoted, the only thing which could throw a NullReferenceException is if obj == null.

Please could you PM me a link to a basic test project and some instructions so I can replicate this at my end?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Shockwolf
Posts: 14
Joined: Thu Feb 09, 2017 7:09 am

Re: Creating an Automatic Save Structure

Post by Shockwolf »

It has something to do with loading between scenes. If I test it without changing scenes, it works. But when I start the game from my menu scene and it loads the main game scene, suddenly this is an issue and I don't get why. :?
Regards, SW.
Shockwolf
Posts: 14
Joined: Thu Feb 09, 2017 7:09 am

Re: Creating an Automatic Save Structure

Post by Shockwolf »

Joel wrote:Hi there,

In the line you quoted, the only thing which could throw a NullReferenceException is if obj == null.

Please could you PM me a link to a basic test project and some instructions so I can replicate this at my end?

All the best,
Joel
Thanks heaps, I'll PM you when my project is done uploading to Dropbox. :)
Regards, SW.
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Re: Creating an Automatic Save Structure

Post by Joel »

Hi there,

Thanks for sending over your project.

You appear to be destroying created objects (i.e. your blocks) but not removing them from the CreatedObjects list after doing so, meaning there are NULL objects in your CreatedObjects list. This most notably happens when you create a block in the space occupied by the player.

For example, if I place a block where the player spawns, reload the game and then spawn, it will load the block but then immediately destroy it because the player is standing in that position.

If you use the UniqueObjectManager.DestroyObject(object) instead of Destroy(object), it will remove the object from the CreatedObjects array automatically.

I replaced this in your BlockManager class and it appears to work fine. Note that you might also need to delete your old save data as it may be corrupt.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Shockwolf
Posts: 14
Joined: Thu Feb 09, 2017 7:09 am

Re: Creating an Automatic Save Structure

Post by Shockwolf »

Yeah, thanks for that Joel, but I still don't think you understand what I am trying to tell you, so I've made a video...

https://youtu.be/H_2BByyk6Qg

And I just need to clarify that the data is always saved and loaded from the main game scene. No actual saving and loading occurs in the menu scene of other wise. It takes you to the main scene and there is where the data is loaded or saved accordingly.
Regards, SW.
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Re: Creating an Automatic Save Structure

Post by Joel »

Thanks for the clarification. I was indeed following the wrong steps to replicate the issue, but I understand now :)

The issue is that when you exit the scene, any objects created in the scene are destroyed by Unity. However, the createdObjects list is static so it isn't destroyed, so when you enter a new scene it will be filled with null values.

Luckily the fix for this is quite simple: add createdObjects.Clear() to the Awake() method of the UniqueObjectManager. i.e.
public void Awake()
{		
	// Allows us to get a static reference to this instance.
	// (Like a singleton)
	mgr = this;
	// Clear the createdObjects list incase we've entered from another scene.
	createdObjects.Clear();
}
Alternatively you could make the createdObjects List non-static, and change the scripts so that they access it through the UniqueObjectManager's static mgr field, but this requires more effort.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Shockwolf
Posts: 14
Joined: Thu Feb 09, 2017 7:09 am

Re: Creating an Automatic Save Structure

Post by Shockwolf »

OMG IT WORKS!!! Thank you so much Joel! :o :o :o :o :o :D :D :D :D :D
Regards, SW.
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Re: Creating an Automatic Save Structure

Post by Joel »

No problem, let me know if you run into any other issues :D
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Shockwolf
Posts: 14
Joined: Thu Feb 09, 2017 7:09 am

Re: Creating an Automatic Save Structure

Post by Shockwolf »

Thanks, I have another...

I have realised that if I use this system to Instantiate an Object that already has other Objects Parented to it, this creates another problem.

It instantiates fine with the Objects parented to it, however upon trying to delete it, I get this error...
Exception: Cannot destroy prefab: No such prefab exists.
UniqueObjectManager.DestroyObject (UnityEngine.GameObject obj) (at Assets/Easy Save 2/Examples/Creating an Automatic Save Structure/UniqueObjectManager.cs:65)
UniqueObjectManager.DestroyObject (UnityEngine.GameObject obj) (at Assets/Easy Save 2/Examples/Creating an Automatic Save Structure/UniqueObjectManager.cs:69)
BlockManager.RegisterHit () (at Assets/_FallenAngel/Scripts/MAIN/BlockManager.cs:38)
BuildingBlocks.MouseButtonHeld () (at Assets/_FallenAngel/Scripts/MAIN/BuildingBlocks.cs:69)
Which leads to this...

Code: Select all

public static void DestroyObject(GameObject obj)
	{
		// Remove prefab from createdPrefabs list, or throw error if it's not in list.
		if(!CreatedObjects.Remove(obj))
			throw new System.Exception("Cannot destroy prefab: No such prefab exists.");
		
		// If destroying a parent object, we also need to destroy it's children.
		foreach(Transform child in obj.transform)
			DestroyObject(child.gameObject);
		// Destroy the object.
		Destroy(obj);
	}
I've tried adding an E2UniqueID to the extra Objects and adding them to the UniqueObjectManager Prefabs Array list in the Inspector. What am I missing?

Thanks.
Regards, SW.
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Re: Creating an Automatic Save Structure

Post by Joel »

Hi there,

You will need to call the DestroyObject method on the GameObject which contains the ES2UniqueID, and then destroy the other GameObjects.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked