@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;
}
}