Creating an Automatic Save Structure
Re: Creating an Automatic Save Structure
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
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
Re: Creating an Automatic Save Structure
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.
Re: Creating an Automatic Save Structure
Thanks heaps, I'll PM you when my project is done uploading to Dropbox.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
Regards, SW.
Re: Creating an Automatic Save Structure
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
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
Re: Creating an Automatic Save Structure
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.
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.
Re: Creating an Automatic Save Structure
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.
All the best,
Joel
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
Re: Creating an Automatic Save Structure
No problem, let me know if you run into any other issues
Re: Creating an Automatic Save Structure
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...
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.
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...
Which leads to this...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)
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);
}
Thanks.
Regards, SW.
Re: Creating an Automatic Save Structure
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
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