Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enhancement: Support DontDestroyOnLoad for Child GameObjects #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

@lfeq
Copy link
Contributor

@lfeq lfeq commented Mar 5, 2025

Description:

Currently, the UnitySingleton implementation does not persist singleton instances across scene loads when they are children of other GameObjects. This limitation arises because DontDestroyOnLoad only works for root GameObjects; it does not prevent destruction of child objects when their parent is destroyed during a scene load.

Proposed Solution:

Introduce a public boolean property, UnparentOnAwake, to the Singleton class. When set to true, this property will unparent the singleton GameObject during the OnInitializing method, ensuring it becomes a root GameObject and is not destroyed during scene transitions.

Implementation:

public abstract class PersistentMonoSingleton<T> : MonoSingleton<T> where T : MonoSingleton<T>
 {
 /// <summary>
 /// if this is true, this singleton will auto detach if it finds itself parented on awake
 /// </summary>
 [Tooltip("if this is true, this singleton will auto detach if it finds itself parented on awake")]
 [SerializeField] private bool UnparentOnAwake = true;
 
 #region Protected Methods
 protected override void OnInitializing()
 {
 if (UnparentOnAwake) {
 transform.SetParent(null);
 }
 base.OnInitializing();
 if (Application.isPlaying)
 {
 DontDestroyOnLoad(gameObject);
 }
 }
 #endregion
 }

Use Case:

In the video "Project Initialization - Unity Architecture," Tarodev, recommends using a preload scene to initialize all the systems your game uses, such as GameManager, SoundManager, etc. The suggested approach includes the following code:

using UnityEngine;
public static class Bootstrapper {
 [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
 public static void Execute() {
 Object.DontDestroyOnLoad(Object.Instantiate(Resources.Load("Systems")));
 }
}

This code aims to load a single prefab named "Systems" that contains child GameObjects handling various behaviors. However, with the current UnitySingleton implementation, if these child GameObjects are singletons, they cannot persist across scenes because DontDestroyOnLoad does not work on child objects. By implementing the proposed UnparentOnAwake property, each system can be its own prefab, set as a child of the "Systems" GameObject, and still persist across scene loads.

Benefits:

  • Allows singleton instances to persist across scenes, even when initially parented to other GameObjects.​
  • Provides flexibility for developers to decide whether the singleton should be unparented or not via the UnparentOnAwake property.​
  • Simplifies the initialization process by allowing multiple systems to be organized under a single prefab, aligning with the preload scene approach recommended in the video.

Copy link
Member

Thank you @lfeq !

I think another great idea would be to add an option for applying DontDestroyOnLoad on the root GameObject itself.

@hasanbayatme hasanbayatme merged commit 0600101 into UnityCommunity:main Mar 5, 2025
Copy link
Contributor Author

lfeq commented Mar 5, 2025

Thank you for merging the recent pull request and for suggesting the addition of an option to apply DontDestroyOnLoad directly to the root GameObject. I believe this enhancement would provide developers with greater flexibility in managing object persistence across scenes. Implementing this feature would allow for more streamlined control over which GameObjects remain active, aligning with best practices in Unity development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /