DontDestroy
Coding Style wiki
DontDestroy is a cross-scene persistent object marker component. In Awake, it renames the object to the Runtime Name configured in the Inspector and calls DontDestroyOnLoad so the object survives scene switches.
| Namespace | OxGKit.Utilities.DontDestroy |
| Type | public class DontDestroy : MonoBehaviour |
| Source | DontDestroy.cs |
using OxGKit.Utilities.DontDestroy;
Attention DontDestroyOnLoad only works on root GameObjects; attach this component to an object at the scene root level (one per object, DisallowMultipleComponent).
Quick Start
- Attach the component to a root object via AddComponentMenu: OxGKit/Utilities/DontDestroy/DontDestroy.
- Configure the Runtime Name in the Inspector (default
DontDestroy); inAwake, the object is renamed automatically and marked persistent.
using OxGKit.Utilities.DontDestroy;
using UnityEngine;
public class DontDestroyDemo : MonoBehaviour
{
private void Start()
{
var dontDestroy = this.GetComponent<DontDestroy>();
// Rename the object at runtime
dontDestroy.SetRuntimeName("GameManager");
}
}
Methods
SetRuntimeName
public void SetRuntimeName(string runtimeName)
Renames the object with the given name (updates both the GameObject name and the internally stored Runtime Name).