Files
Emberwild/Assets/GAME/Script/Core/Persistence.cs
T
2026-06-22 16:18:34 +02:00

27 lines
969 B
C#

using UnityEngine;
namespace Ashwild.Core
{
/// <summary>
/// Helper for managers that must outlive scene loads. It detaches the object to the scene root
/// (DontDestroyOnLoad only works on root objects) and marks it persistent — call it from Awake.
///
/// This is what lets a persistent manager sit under a tidy "Managers" parent in the editor next
/// to NON-persistent ones: only the objects that call Persist leave the parent and survive across
/// scenes; the rest stay children of the parent and are destroyed with the scene as usual.
/// </summary>
public static class Persistence
{
/// <summary>
/// Moves the GameObject to the scene root and keeps it alive across scene loads.
/// </summary>
public static void Persist(GameObject go)
{
if (go == null) return;
go.transform.SetParent(null);
Object.DontDestroyOnLoad(go);
}
}
}