Advertisement
salahzar

DontDestroy.cs

Feb 13th, 2021
1,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // Object.DontDestroyOnLoad example.
  6. //
  7. // This script example manages the playing audio. The GameObject with the
  8. // "music" tag is the BackgroundMusic GameObject. The AudioSource has the
  9. // audio attached to the AudioClip.
  10.  
  11. public class DontDestroy : MonoBehaviour
  12. {
  13.     private static GameObject _instance;
  14.  
  15.     void Awake()
  16.     {
  17.         //if we don't have an [_instance] set yet
  18.         if (!_instance)
  19.         {
  20.             _instance = gameObject;
  21.             DontDestroyOnLoad(gameObject);
  22.         }
  23.         //otherwise, if we do, kill this thing
  24.         else {
  25.             Destroy(gameObject);
  26.  
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement