gpt4 book ai didi

c# - 使用不同的副本访问 Unity 中的游戏对象

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:25 27 4
gpt4 key购买 nike

我正在我的 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 脚本中。这是一些屏幕截图:

enter image description here enter image description here

在接受按钮上,我在附加到播放器的 QuestTracker 上使用了 acceptQuest() 函数,但我无法为 QuestGiver 设置特定值,因为我有多个 NPC 副本,而不仅仅是一个。

enter image description here

我想要的是通过运行时在播放器上设置 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com