我正在我的 RPG(角色扮演游戏)中创建一个任务系统,它可以通过 NPC(非玩家角色)获得。我在我的主角身上附上了一个脚本,可以检查你是否接到了 NPC 的任务。代码如下:
public class QuestTracker : MonoBehaviour
{
public QuestGiver questGiver;
public Button AcceptQuest;
public OpenQuestWindow questWindow;
public void acceptQuest()
{
questGiver.questAccepted = true;
}
}
现在我已经在我的 NPC 上附加了一个脚本,以便他们向玩家发出任务。这是 NPC 的代码:
public class QuestGiver : MonoBehaviour
{
public bool questAccepted = false;
}
当玩家点击 NPC 时,会出现一个窗口,向玩家显示他/她的任务目标。目前,我已经创建了 2 个 NPC,并为他们附加了 QuestGiver 脚本。这是一些屏幕截图:
在接受按钮上,我在连接到玩家的 QuestTracker 上使用了 AcceptQuest() 函数,但我无法为 QuestGiver 设置特定值,因为我有多个 NPC 副本,而不仅仅是一个。
我想要的是通过运行时在播放器上设置 QuestGiver。我知道它可以通过使用 OnMouseOver() 函数或 Raycast 来实现。我知道逻辑,但不知道如何实现。
最佳答案
我认为使用静态变量可以解决你的问题。将玩家 questGiver 设置为静态。
public class QuestTracker : MonoBehaviour
{
public static QuestGiver questGiver;
public Button AcceptQuest;
public OpenQuestWindow questWindow;
public void acceptQuest()
{
questGiver.questAccepted = true;
}
}
然后当NPC发出任务时,通过Npc的脚本更改玩家的questGiver。
void OnMouseDown()
{
QuestTracker.questGiver = this;
}
编辑:顺便说一句,当您将其更改为静态时,您将不会在检查器中看到 questGiver 变量。使用 Debug.Log() 进行测试。
关于c# - 在 Unity 中访问具有不同副本的游戏对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32165082/