我真的很笨,但我认为我是雪盲。从另一个优雅的调用中,我无法访问单例类方法。我得到了可怕的访问一个单例返回(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