C# 错误CS0120:访问非静态成员'questgiver.iscompleted\u questgiver'需要对象引用
我正试图从一个名为Jenny_NPC的游戏对象中访问一个名为questgiver的脚本。然而,每当我访问一个名为iscompleted_questgiver的非静态布尔变量时,我就有一个错误。我浏览并搜索了其他人收到的相同错误,但我似乎找不到与我现在经历的完全相同的情况C# 错误CS0120:访问非静态成员'questgiver.iscompleted\u questgiver'需要对象引用,c#,unity3d,C#,Unity3d,我正试图从一个名为Jenny_NPC的游戏对象中访问一个名为questgiver的脚本。然而,每当我访问一个名为iscompleted_questgiver的非静态布尔变量时,我就有一个错误。我浏览并搜索了其他人收到的相同错误,但我似乎找不到与我现在经历的完全相同的情况 void Show_Quest_Update() { GameObject Jennny_Quest = GameObject.FindGameObjectWithTag ("Jenny_NPC"); Jenn
void Show_Quest_Update()
{
GameObject Jennny_Quest = GameObject.FindGameObjectWithTag ("Jenny_NPC");
Jennny_Quest.GetComponent<questgiver>();
Quest_List ();
if(questgiver.iscompleted_questgiver == false)
{
title_index = 0;
infos_index = 0;
npc_index = 0;
}
quest_title = quest [title_index, infos_index];
quest_objectives = quest [title_index, infos_index + 3];
quest_rewards = quest [title_index, infos_index + 4];
quest_target = npc [npc_index];
Panel ();
}
您需要创建questgiver类的实例,以便在当前脚本中访问它
void Show_Quest_Update()
{
GameObject Jennny_Quest = GameObject.FindGameObjectWithTag ("Jenny_NPC");
questgiver myQuestGiver = Jennny_Quest.GetComponent<questgiver>();
Quest_List ();
if(myQuestGiver.iscompleted_questgiver == false)
{
title_index = 0;
infos_index = 0;
npc_index = 0;
}
quest_title = quest [title_index, infos_index];
quest_objectives = quest [title_index, infos_index + 3];
quest_rewards = quest [title_index, infos_index + 4];
quest_target = npc [npc_index];
Panel ();
}
排队
Jennny_Quest.GetComponent<questgiver>();
您想访问类成员iscompleted\u questgiver,它必须是静态的
该错误存在于任务给定者的定义中,没有使用static标记iscompleted_questgiver
将其设置为静态将完全改变该对象成员到类成员的性质,并且可能不是您所期望或需要的
准确回顾您正在做的事情,因为似乎存在语义理解问题。可能GetComponent方法返回对对象的引用,您需要将该引用分配给变量。。。只是一个想法。你需要创建一个questgiver的实例来达到它的iscompleted_questgiver成员,因为它不是静态的。喜欢var实例=新的任务给予者;我已经完成了任务,请用勺子喂我。在你的代码中,questgiver被用作脚本的类名类型。您应该这样做:questgiver qg=jenny_Quest.GetComponent;天哪!非常感谢詹姆斯·霍格尔!救恩来自你。我爱你!
if(questgiver.iscompleted_questgiver == false)