【问题标题】:Why my dialouge system won't load the 2nd sentences on Unity?为什么我的对话系统不会在 Unity 上加载第二个句子?
【发布时间】:2022-10-11 16:23:01
【问题描述】:

我的代码有这个问题,因为当我按下 E 说话时,它可以很好地工作,显示画布和继续按钮,但是当我按下继续按钮时,它不会显示下一句。

这是代码:

using UnityEngine;
using TMPro;
using System.Collections;

public class Dialogue : MonoBehaviour
{
    public TextMeshProUGUI textDisplay;
    public string[] sentences;
    private int index;
    public float typingSpeed;
    public bool playerIsClose;
    public GameObject dialoguePanel;
    public GameObject continueButton;

    private void Update ( )
    {
        if ( Input.GetKeyDown ( KeyCode.E ) && playerIsClose )
        {
            continueButton.SetActive ( true );
            if ( dialoguePanel.activeInHierarchy )
            {
                zeroText ( );
            }
            else
            {
                dialoguePanel.SetActive ( true );
            }
        }
    }

    public void zeroText ( )
    {
        dialoguePanel.SetActive ( false );
    }

    private void OnTriggerEnter2D ( Collider2D other )
    {
        if ( other.CompareTag ( "Player" ) )
        {
            playerIsClose = true;
        }
    }

    private void Start ( )
    {
        StartCoroutine ( Type ( ) );
    }

    IEnumerator Type ( )
    {
        foreach ( char letter in sentences [ index ].ToCharArray ( ) )
        {
            textDisplay.text += letter;
            yield return new WaitForSeconds ( typingSpeed );
        }
    }

    public void NextSentence ( )
    {
        continueButton.SetActive ( false );

        if ( index < sentences.Length - 1 )
        {
            index++;
            textDisplay.text = " ";
            StartCoroutine ( Type ( ) );
        }
        else
        {
            zeroText ( );
        }
    }

    private void OnTriggerExit2D ( Collider2D other )
    {
        if ( other.CompareTag ( "Player" ) )
        {
            playerIsClose = false;
            zeroText ( );
        }
    }
}

我还尝试将index++; 放在其他地方,并把zeroText () 尝试看看它是否真的是问题,结果它仍然坏了。

【问题讨论】:

  • 我很惊讶你在调用方法 Type 时没有遇到问题。然而。你永远不会叫下一句所以......

标签: unity3d


【解决方案1】:

数组中存储了多少个句子?我猜是两个?!看看NextSentence() 中的if 语句:从0 开始的index 将与数组的length 进行检查-1.这将始终导致数组中的最后一个句子被忽略。

删除 -1 或将 < 更改为 <=。

其他人注意到你从不使用NextSentence(),但我猜你是通过按钮来做的?!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2023-03-03
    • 2018-09-08
    相关资源
    最近更新 更多