unity dont destroy on load warning code example
Example 1: dont destroy on load unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
{
void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("music"); if (objs.Length > 1)
{
Destroy(this.gameObject);
} DontDestroyOnLoad(this.gameObject);
}
}
Example 2: unity clone don't destroy on scene load
private static Player playerInstance;
void Awake(){
DontDestroyOnLoad (this);
if (playerInstance == null) {
playerInstance = this;
}
else {
DestroyObject(gameObject);
}
}