我正在创建一个脚本来控制对话并为 Unity 游戏中的主要角色分配任务。有一个任务列表,根据列表中变量的数量,可以改变对话。如果变量等于 1 主角得到一个任务,2 任务完成,3 主角告诉 npc 任务完成。当触发 npc 碰撞器时,会出现对话图像,玩家会看到 npc 的文字和 2 个带有可能答案的按钮。一个按钮关闭对话图像,另一个按钮进一步移动对话。
问题是我找不到让它工作的方法( tasks[0] == 3 )。因为它不会改变和对话循环。我试着把它放在不同的地方,但没有任何改变。
我是初学者,所以可能会有一些我没有注意到的愚蠢错误。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dialogue : MonoBehaviour
{
[SerializeField] GameObject dialogue;
[SerializeField] Text npcText;
[SerializeField] List<Text> buttonText = new List<Text>();
public DialogueBlock[] dialogueBlock;
[SerializeField] public PlayerController playerController;
int dialogueBlockPlace = 0;
public List<int> tasks = new List<int>();
[SerializeField] List<string> taskString = new List<string>();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
dialogue.SetActive(true);
dialogueBlockPlace = 0;
Answer(0);
}
}
public void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
dialogue.SetActive(false);
}
}
private void Answer(int num)
{
npcText.text = dialogueBlock[num].npcText;
buttonText[0].text = dialogueBlock[num].playerAnswer[0].Text;
buttonText[1].text = dialogueBlock[num].playerAnswer[1].Text;
}
public void AnswerButtonClicked(int buttonNum)//u=mesti ant mygtuko
{
if(dialogueBlock[dialogueBlockPlace].playerAnswer[buttonNum].speakEnd)
{
dialogue.SetActive(false);
}
dialogueBlockPlace = dialogueBlock[dialogueBlockPlace].playerAnswer[buttonNum].toBlock;
TaskStarted();
if (dialogueBlock[dialogueBlockPlace].playerAnswer[buttonNum].questInt == 1)
{
if (tasks[0] == 0)
{
tasks[0] = 1;
}
else if (tasks[0] > 2)
{
tasks[1] = 1;
}
else if (tasks[1] > 2 )
{
tasks[2] = 1;
}
}
DialogueControl();
}
public void DialogueControl()
{
TaskDone();
ChangeText();
print(dialogueBlockPlace);
Answer(dialogueBlockPlace);
}
private void TaskStarted()
{
if (tasks[0] == 1 || tasks[1] == 1 || tasks[2] == 1)
{
dialogueBlockPlace = 4;
}
}
private void TaskDone()
{
if (tasks[0] == 2 || tasks[1] == 2 || tasks[2] == 2)
{
dialogueBlockPlace = 3;
tasks[0] = 3;//dont work
}
}
public void ChangeText()
{
if (tasks[0]==0)
{
dialogueBlock[2].npcText = taskString[0];
}
if (tasks[0] == 3)
{
dialogueBlock[2].npcText = taskString[1];
}
if (tasks[1] == 3)
{
dialogueBlock[2].npcText = taskString[2];
}
}
}
[System.Serializable]
public class DialogueBlock
{
public string npcText;
public PlayerAnswer[] playerAnswer;
}
[System.Serializable]
public class PlayerAnswer
{
public string Text;
public int toBlock;
public bool speakEnd;
public int questInt;
}