字符串数组未递增

时间:2018-08-28 16:45:11

标签: c# arrays unity3d

我可以从主脚本(RPGDialogueManager.cs)轻松访问此字符串数组,并且该值正常显示,但是当我尝试通过其他脚本(RPGDialogueHolder.cs)访问此数组时,更改其值并更改“ lineNum”恢复为0,则不会显示该值。它只是发生故障(它显示索引1的值,但它很快会恢复为默认行“嘿,您是怎么了?”)

  

注意:RPGDialogueHolder.cs中的 dialogueLines 与   RPGDialogueManager.cs中的 dialogueLines 。两个字符串数组都有   彼此不同的值

RPGDialogueManager.cs

    void Start () {
    dialogueBox.SetActive(true);
    directionalButtons.SetActive(false);
    dialogueText.text = "In the middle of the day, this two young adults volunteer to help and leads me to their town";

    nextButton.onClick.AddListener(DialogueLines);

}

public void DialogueLines()
{

    if (lineNum >= dialogueLines.Length)
    {
        dialogueBox.SetActive(false);
        directionalButtons.SetActive(true);
        lineNum = 0;
    }
    dialogueText.text = dialogueLines[lineNum];
    lineNum++;
}    

public void ShowDialogue()
    {
        dialogueBox.SetActive(true);
        directionalButtons.SetActive(false);
    }

RPGDialogueHolder.cs

    void OnTriggerStay2D(Collider2D collider2D)
{
    if (collider2D.gameObject.name == "Player")
    {
        // dialogueManager.dialogueText.text = dialogue;
        dialogueManager.dialogueText.text = "Hey you, What makes you go here?";
        dialogueManager.dialogueLines = dialogueLines;
        dialogueManager.lineNum = 0;
        aButton.onClick.AddListener(dialogueManager.ShowDialogue);
    }
}

1 个答案:

答案 0 :(得分:2)

如果播放器位于触发器内,则每帧称为

OnTriggerStay2D。因此,您实际上将对话重置为Hey you,...每帧。

尝试将回调从OnTriggerStay2D更改为OnTriggerEnter2D或使用其他方法进行对话