Suggested improvement to Persisting Objects

@Brian_Trotter let me know your thoughts.

I would suggest it is a strict improvement to save the PersistentObjects prefab in a Resources folder in the project so that it can be loaded at runtime by the resources subsystem, then doing the following.

This prevents the cascading ugliness of having to move any Find<> calls to Start() in other behaviours (which prevents things like subscribing to events in OnEnable())

    public class PersistentObjectSpawner : MonoBehaviour
    {
        private static GameObject _persistentObjectsPrefab;

        private static bool _hasSpawned;

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        private static void Init()
        {
            _hasSpawned = false;
            _persistentObjectsPrefab = Resources.Load<GameObject>("PersistentObjects");
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        private static void Awake()
        {
            if (_hasSpawned) return;
            var persistentObjects = Instantiate(_persistentObjectsPrefab);
            DontDestroyOnLoad(persistentObjects);
            _hasSpawned = true;
        }
    }

This is intriguing.

I would change the names of the methods themselves to avoid confusion (despite being static, folks whose only exposure to C# is through Unity might confuse the timing of the static Awake(). When it comes to handlers like these, I prefer to be very verbose with the names

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        private static void SubsystemRegistration()
        {
            _hasSpawned = false;
            _persistentObjectsPrefab = Resources.Load<GameObject>("PersistentObjects");
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        private static void BeforeSceneLoad()
        {
            if (_hasSpawned) return;
            var persistentObjects = Instantiate(_persistentObjectsPrefab);
            DontDestroyOnLoad(persistentObjects);
            _hasSpawned = true;
        }

Good pickup, I wasn’t sure if the way that reflection is done would still require the same method names. I prefer it your way too.

Hopefully this represents an improvement, glad to have made a small contribution to the course :smiley:

I have to say this type of solution was the first thing i have try , then to realize that is working on Unity but not on a Build. I am working at something similar if you want to check is on Unity Store and is free since at the moment is still work in progress . Is called Save Instantiated Game Objects , and at the moment works for what i can say but before i continue i wait for the Entities system of unity to get stable since in last years have change again and again and in my opinion should bring a change to entire system.

Interesting, I haven’t run a build in a while. That’s a good reminder to test production environment regularly!

I’ve since moved to a system that uses ScriptableObject based EventBus architecture, following the example of UOP1 and Unity’s best practices ebooks.

Privacy & Terms