public static Core S = null;
private bool DontDestroyOnLoad = true;
void Awake()
{
//防止多次声明
if (S != null)
{
GameObject.Destroy(this.gameObject);
return;
}
S = this;
if (DontDestroyOnLoad)
GameObject.DontDestroyOnLoad(this);
Debug.Log("CoreAwake");
}
在做扫雷的益智小游戏中,发现没加判断会多次声明出Core 物体;
原先的写法很简洁的单例写法。
public static Core S ;
void Awake()
{
S = this;
GameObject.DontDestroyOnLoad(this);
}
造成多次出现了,Core的预制体,所以,我们在什么这种不销毁的物体的时候,一定要加判断。防止多次出现。