Skip to main content

DontDestroy

Important Attention Reminder

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.

NamespaceOxGKit.Utilities.DontDestroy
Typepublic class DontDestroy : MonoBehaviour
SourceDontDestroy.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

  1. Attach the component to a root object via AddComponentMenu: OxGKit/Utilities/DontDestroy/DontDestroy.
  2. Configure the Runtime Name in the Inspector (default DontDestroy); in Awake, 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).