Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dukious25 · Jun 04 at 06:24 AM · 2d game

How to fix dialog system looping error 2D unity?

I'm following a tutorial on youtube(https://www.youtube.com/watch?v=1nFNOyCalzo) on how to make a simple dialogue system that when you collide on the NPC and press E a dialogue box with dialog and a continue button will appear, I changed it a bit; instead of pressing E, I try assigning it on a button because I'm making a mobile game so I make a public void InteractButton(). the problem is instead of displaying the line or sentence it loops and blinks like a Christmas light and it does not show the text properly, if I put Help it shows elpH. what should I do?

here's the code that I put on NPC

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

public class DialogueManager : MonoBehaviour {

 public string[] dialogue;
 private int index;
 public GameObject dialoguePanel;
 public TextMeshProUGUI dialogueText;
 public GameObject continueButton;
 public float typingSpeed;
 public bool playerIsClose;
 public bool isClick;

 // Update is called once per frame
 void Update()
 {
     if (isClick && playerIsClose) 
     {
         if (dialoguePanel.activeInHierarchy) 
         {
             zeroText();
         }
         else 
         {
             dialoguePanel.SetActive(true);
             StartCoroutine(Type());
         }
     }

     if (dialogueText.text == dialogue[index])
     {
         continueButton.SetActive(true);
     }
 }

 public void zeroText() 
 {
     dialogueText.text = "";
     index = 0;
     dialoguePanel.SetActive(false);
 }

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

 public void NextSentence()
 {
     continueButton.SetActive(false);
     if (index < dialogue.Length - 1)
     {
         index++;
         dialogueText.text = "";
         StartCoroutine(Type());
     }
     else
     {
         zeroText();
     }
 }

 public void InteractButton() 
 {
     isClick = true;
 }

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

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Jun 04 at 04:02 PM

  // Update is called once per frame
  void Update()
  {
  }
 
  public void InteractButton() 
  {
      if (playerIsClose) 
      {
          if (dialoguePanel.activeInHierarchy) 
          {
              zeroText();
          }
          else 
          {
              dialoguePanel.SetActive(true);
              StartCoroutine(Type());
          }
      }
  }
 
  IEnumerator Type()
  {
      WaitForSeconds wait = new WaitForSeconds(typingSpeed);
      foreach (char letter in dialogue[index].ToCharArray())
      {
          dialogueText.text += letter;
          yield return wait;
      }
      continueButton.SetActive(true);
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dukious25 · Jun 05 at 04:07 AM 0
Share

man you're awesome I've been struggling for about 3 days but it fixes instantly by following your script, thank you so much man you're a lifesaver.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My sound effect is not playing 0 Answers

Tooltip doesn't work 0 Answers

Object keeps bobbing between two tiles 0 Answers

How to make the next level load based on 2 characters touching a collider? 0 Answers

2d Roguelike tutorial error: "The type or namespace name `BoardManager' could not be found" 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges