• 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 IgorArnaut · Aug 27, 2022 at 08:36 AM · dialogue

How to start dialog on differently and continue as long as player presses "E" key?

Hello. I have made a universal Dialog Manager that has DialogManager script. In one scene I have "Terminal" GameObject that starts dialog when player is in range and presses "E" and in the Battle scene dialog starts automatically when the player wins or loses a battle (condition in BattleManager script).

Terminal.cs:

 public class Terminal : MonoBehaviour
 {
     private Animator anim;
 
     private GameObject clue;
 
     [SerializeField]
     private DialogManager manager;
     [SerializeField] 
     private string[] lines;
     private bool inRange;
 
     // Use t$$anonymous$$s for initialization
     void Start()
     {
         anim = GetComponent<Animator>();
 
         clue = GameObject.FindGameObjectWithTag("Player").transform.GetC$$anonymous$$ld(0).gameObject;
     }
 
     // Update is called once per frame
     void Update()
     {    
         if (inRange && Input.GetKeyDown(KeyCode.E))
             manager.Write(lines);
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("Player"))
         {
             inRange = true;
 
             anim.SetBool("inRange", true);
             clue.SetActive(true);
             clue.GetComponent<Animator>().SetInteger("clue", 1);
         }
     }
 
     private void OnTriggerExit2D(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("Player"))
         {
             inRange = false;
 
             anim.SetBool("inRange", false);
             clue.SetActive(false);
             clue.GetComponent<Animator>().SetInteger("clue", 0);
             manager.textBox.SetActive(false);
         }
     }
 }

BattleManager.cs:

 public class BattleManager : MonoBehaviour
 {
     private string[] defeat = { "You are defeated!" };
     private string[] victory = { "Enemy has been defeated!", "You are victorius!" };
 
     private GameObject player;
     private GameObject enemy;
 
     [SerializeField]
     private DialogManager dManager;
     [SerializeField]
     private SceneManager2 sManager;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         enemy = GameObject.FindGameObjectWithTag("Enemy");
     }
 
     void Update()
     {
         if (player == null) {
             Debug.Log("You have lost!");
             dManager.Write(defeat);
         }
 
         if (enemy == null) {
             Debug.Log("You have won!");
             dManager.Write(victory);
         }
     }
 }

Here is my attempt, but I get index out of bounds for some reason.

DialogManager.cs:

 public class DialogManager : MonoBehaviour
 {
     public GameObject textBox;
     private TMP_Text dialogText;
     
     private bool running;
 
     [SerializeField]
     private int index = -1;
 
     void Start()
     {
         textBox = transform.GetC$$anonymous$$ld(0).gameObject;
         dialogText = textBox.GetComponentInC$$anonymous$$ldren<TextMeshProUGUI>();
     }
 
     public void Write(string[] lines)
     {
         textBox.SetActive(true);
         StartCoroutine(Write(lines[++index]));
 
         if (Input.GetKeyDown(KeyCode.E) && !running)
         {
             if (textBox.activeInHierarchy && index > lines.Length - 2)
             {
                 textBox.SetActive(false);
                 index = -1;
             }
             else
             {
                 if (index < lines.Length)
                     StartCoroutine(Write(lines[++index]));
             }
         }
     }
 
     private IEnumerator Write(string line)
     {
         running = true;
         dialogText.text = "";
 
         foreach (char c in line)
         {
             dialogText.text += c;
             yield return new WaitForSeconds(0.01f);
         }
 
         running = false;
     }
 }






Comment
Add comment · Show 1
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 IgorArnaut · Aug 27, 2022 at 04:47 PM 0
Share

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

188 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

Related Questions

Inserting specific words into descriptions stored in XML 0 Answers

A (X) amount of time between "dialogues" strings 1 Answer

Best Practice For Storing RPG Dialogue? 1 Answer

Dialogue et personnalisation 0 Answers

Add an an array of images to use in dialogue box 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