gpt4 book ai didi

c# - gameObject 的多个实例表现不尽相同

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

所以我的游戏世界中有三个不同的“旁观者”,每个都有一个附加的“旁观者”脚本,并且我的 UI 中有一个“旁观者对话”元素。这个想法是,当玩家进入任何旁观者的范围时,会显示从数据库中随机选择的文本,但对于我的脚本,它只对其中一个旁观者有效。

我觉得我交叉引用脚本太多了,但我不知道。下面是两部分代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Bystander : MonoBehaviour {

GameObject player;
public bool speak;
public bool isPlayerNear;
public int dialogueNumber;
GameObject bystanderDialogueObject;
BystanderDialogue bystanderDialogue;


// Use this for initialization
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
bystanderDialogueObject = GameObject.Find ("BystanderDialogue");
bystanderDialogue = bystanderDialogueObject.GetComponent<BystanderDialogue> ();
}

// Update is called once per frame
void Update ()
{

}

void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
dialogueNumber = Random.Range (0, bystanderDialogue.bystanderSpeech.Length);
speak = true;
isPlayerNear = true;
}
}

void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
speak = false;
isPlayerNear = false;
}
}
}

第二个附加到 UI 对象“BystanderDialogue”:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BystanderDialogue : MonoBehaviour {

Text text;
GameObject[] bystanderObjects;
Bystander bystander;
public string[] bystanderSpeech;
// Use this for initialization
void Awake ()
{
text = GetComponent<Text> ();
bystanderObjects = GameObject.FindGameObjectsWithTag ("NPC");
foreach (GameObject bystanderObject in bystanderObjects)
{
bystander = bystanderObject.GetComponent<Bystander> ();
}
}

// Update is called once per frame
void Update ()
{
BystanderSpeak (bystander);

if (bystander.isPlayerNear)
{
Debug.Log ("New bystander!");
}

}

void BystanderSpeak(Bystander bystander)
{
if (bystander.speak && bystander.isPlayerNear)
{
text.text = bystanderSpeech[bystander.dialogueNumber];
}
else if (!bystander.speak && !bystander.isPlayerNear)
{
text.text = "";
}
}
}

我很确定我犯了一些基本错误,所以很抱歉。非常感谢任何帮助!

最佳答案

这段代码没有意义:

void Awake () 
{
text = GetComponent<Text> ();
bystanderObjects = GameObject.FindGameObjectsWithTag ("NPC");
foreach (GameObject bystanderObject in bystanderObjects)
{
bystander = bystanderObject.GetComponent<Bystander> ();
}
}

如果有多个 bystanderObject,那么您只保留最后一个对象作为 bystander 成员字段的引用。您应该使用集合(列表或数组)来保留所有这些:

Bystander[] bystanders;


void Awake()
{
bystanderObjects = GameObject.FindGameObjectsWithTag ("NPC");
bystanders = new Bystander[bystanderObjects.Length];
for (var i = 0; i < bystanderObjects.Length; ++i)
{
bystander[i] = bystanderObjects[i].GetComponent<Bystander>();
}
}

编辑:您似乎误解了 Unity 中两帧之间的一切运作方式。我建议你阅读 Execution Order of Event Functions .

您的 Update() 方法将在屏幕上绘制任何内容之前完成。因此,如果您覆盖 Text 的值,则只会显示最后写入的值。事实上,每个 bystander 都需要一个 Text 实例。

关于c# - gameObject 的多个实例表现不尽相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630338/

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