Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

c# - No Continue Button is displayed even after the Dialogue is done in Unity

I don't want the player to spam the continueButtonSE hence, I only want it to appear after the dialogue has finished.

It works in the first index but for the next element, the continueButton does not appear. Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class DialogueSE : MonoBehaviour
{
   public TextMeshProUGUI textDisplaySE;
   public string[] sentencesSE;
   private int index;
   public float typingSpeedSE;
   public GameObject continueButtonSE;

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

   void Update()
   {
     if(textDisplaySE.text == sentencesSE[index])
     {
         continueButtonSE.SetActive(true);
     }
   }

   IEnumerator Type()
   {
     foreach (char letter in sentencesSE[index].ToCharArray())
     {
         textDisplaySE.text += letter;
         yield return new WaitForSeconds(typingSpeedSE);
     }
   }
   public void NextSentenceSE()
   {
     continueButtonSE.SetActive(false);
    
     if (index < sentencesSE.Length - 1)
     {
         index++;
         textDisplaySE.text = " ";
         StartCoroutine(Type());
     }
     else
     {
         textDisplaySE.text = " ";
         continueButtonSE.SetActive(false);
     }
   }
}

I've disabled the continueButtonSE from the start so that it can only appear once sentencesSE[index] is done appearing.

enter image description here

enter image description here

enter image description here enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are prepending a space when you say textDisplaySE.text = " "; and then you add things in the coroutine therefore if(textDisplaySE.text == sentencesSE[index]) is never true because your in the second case textDisplaySE.text is actually [space]test2 instead of just test2 which is what sentencesSE[index].

On line 31 instead you can initialize to textDisplaySE.text = string.Empty; to not have any whitespace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...