Complete the code to prevent the GameObject from being destroyed when loading a new scene.
void Awake() {
[1](gameObject);
}The DontDestroyOnLoad method keeps the GameObject alive across scene loads.
Complete the code to ensure only one instance of the GameObject persists using DontDestroyOnLoad.
void Awake() {
if (FindObjectsOfType<[1]> ().Length > 1) {
Destroy(gameObject);
} else {
DontDestroyOnLoad(gameObject);
}
}This code checks if more than one GameManager exists and destroys duplicates, keeping only one persistent instance.
Fix the error in the code that tries to use DontDestroyOnLoad incorrectly.
void Start() {
[1];
}The DontDestroyOnLoad method requires a GameObject as parameter, so this.gameObject is correct.
Fill both blanks to create a singleton pattern that uses DontDestroyOnLoad correctly.
public class GameManager : MonoBehaviour { private static GameManager [1]; void Awake() { if ([2] != null && [2] != this) { Destroy(gameObject); return; } [2] = this; DontDestroyOnLoad(gameObject); } }
The static variable instance holds the singleton reference. The code checks if instance exists and assigns this to it if not.
Fill all three blanks to complete the code that prevents multiple instances and uses DontDestroyOnLoad.
public class AudioManager : MonoBehaviour { private static AudioManager [1]; void Awake() { if ([2] != null && [2] != this) { [3](gameObject); return; } [2] = this; DontDestroyOnLoad(gameObject); } }
The static variable instance is used to track the singleton. Duplicate objects are destroyed using Destroy(gameObject).