我真的很笨,但我認爲我是雪盲。從另一個優雅的調用中,我無法訪問單例類方法。我得到了可怕的訪問一個單例返回(NullReferenceException)
(NullReferenceException)。
這裏是我的簡單單身人士,以及我如何調用方法。
public class PlayerNodePosition : MonoBehaviour
{
public static PlayerNodePosition instance;
string code;
void Awake()
{
if (instance == null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
public void AddCode(string _code)
{
code = _code;
}
}
這裏是來自另一個腳本的調用者。
void AddCode()
{
PlayerNodePosition.instance.AddCode("Added!");
}
是一個「傻瓜」我很明顯缺少明顯的。
除了答案,你可能會發現這篇文章:http://www.c-sharpcorner.com/UploadFile/8911c4/singleton-design-pattern-in-C-Sharp /有用的 – 4D1C70