Does anyone know how to reset a game object to it’s first state when you loaded the level?
By that I mean: Reset all variables on scripts (perhaps with reflection?) and generally reset all other components including stuff like the transform.
I now have my scenes as prefabs which I then instantiate/destroy, but it is still a bit slow, so I was looking for an even more efficient way to do it. It’s a small game with small scenes (total 450kB), therefore it makes most sense to keep the scenes in memory and then have a much faster load time. If I’m already preloading the scenes then I might as well have them permanently in the scene instead of instantiating them.
I know that you could on every single script make a method to store/reset the variables that you want to reset, but I was thinking if there was an easier way.
Also I want to know, how much faster would this be than what I am currently doing? Because if it turns out I have to write all these reset methods it better be a huge improvement.
That information would have to be tokenized somewhere so that you can actually set the values to what they were in the first place.
Another problem is that some values in the scripts may or may not need to be set. Say you have a private unserialized field in a script that counts the number of some action it performed, this might need to be reset to 0. But what if another is an unserialized List that is created during Awake… this shouldn’t be touched, since touching it would destroy its contents, and Awake won’t ever happen again.
So not only do you need to tokenize all the values, you need to determine what values should be tokenized, and which shouldn’t. This is all arbitrary, and on a script by script basis. So it’s easiest to let the script decide how it should be done.
This can get complicated.
And the resetting can be slow and memory intensive.
Because here’s the thing, unity already has a token of your GameObjects. It’s the serialized prefab (or scene) of that GameObject. And the ‘Instantiate’ call is what is deserializing it (setting the tokenized version onto the GameObject).
Might as well just let unity do it. If the project is so small, it probably won’t be that big of a difference in speed. Furthermore, unity has a asynchronous model for loading scenes… where as when you modify the components on the GameObjects, you’ll need to do it on the main thread (not async) because you might end up modifying a property that touches the unity API (for instance a collider, or rigidbody or something).
Okay thanks yeah I thought so, the risk of having an unstable, laggy, buggy game and a lot of work to do for not that much improvement doesn’t really add up.
I have a question. The first time you instantiate a prefab it will take longer. I have a class for preloading all my prefabs, but I’m not sure if it’s working. Is this right? I’m using Resources.UnloadUnusedAssets() in between “scenes”, so it might unload it again, do I need a reference to the instantiated prefabs for them to remain in memory?
using UnityEngine;
using System.Collections.Generic;
public class PrefabPreloader : MonoBehaviour {
public List<GameObject> prefabs = new List<GameObject>();
void Awake () {
GameObject instantiated;
foreach(GameObject prefab in prefabs)
{
instantiated = Instantiate(prefab) as GameObject;
instantiated.SetActive(true);
instantiated.SetActive(false);
Destroy(instantiated);
}
}
}
So… my script preloads the prefabs right? I’ll be using these prefabs elsewhere in other scripts no references taking from the preloading script, this script is just supposed to load the prefabs into memory before the actual scripts that are going to use them will instantiate them, to avoid hiccups on first instantiate during gameplay.