Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xandymonzen · Mar 30, 2020 at 11:33 AM · arraycoroutinestringdialogue

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

Hello everyone, it's my first time here, so if I'am posting that on the wrong place or something like that, please tell me.

I'am trying to put some interval between the dialogues that I will put on Editor, instead of press some key to advance the dialogue, I want to put like "10" seconds of duration for every string in dialogue[].

So, it's basically about dialogue script, that when the "Player" enter on a Trigger2D zone activate the following script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Active_Dialog : MonoBehaviour
 {
     public string[] dialogue;
     public Text output;
     private int curLine;
     
     void Start()
     {
         output.enabled = false;
     }
 
     void OnTriggerEnter2D(Collider2D col)
     { 
         if (col.CompareTag("Player"))
         {
             output.enabled = true;
             output.text = dialogue[0];
         }
     }
 
     void OnGUI()
     {
         if (output.enabled == true)     
         {
             curLine++;
             if (curLine < dialogue.Length)
             {
                 output.text = dialogue[curLine];
             }
             else
             {
                 curLine = 0;
                 output.enabled = false;
             }
         }
     }

I already tried somethings like put a Coroutine, I tried in many lines to put a Coroutine with differentes blocks of code, the max that I got was the first and the second string of the array stay the amount of time I wanted. But I still messy about how to use Coroutine (since everywhere say a different thing to another one) and I'am pretty sure that I dont got it the idea of "pause and execute the next line of the code in x amount of seconds" yet... So if someone could explain how I can make that, I will be pretty thankful!

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
0

Answer by The_Three_Vs · Mar 31, 2020 at 07:55 PM

Have you tried using a boolean to track whether the coroutine is running? For example:

 void OnGUI()
      {
          if (output.enabled == true && timerRunning == false)     
          {
              StopAllCoroutines();
              curLine++;
              if (curLine < dialogue.Length)
              {
                  output.text = dialogue[curLine];
                  StartCoroutine(Timer(10f));
              }
              else
              {
                  curLine = 0;
                  output.enabled = false;
              }
          }
      }
 
 private bool timerRunning = false;
 IEnumerator Timer(float time)
 {
     timerRunning = true;
     yield return new WaitForSeconds(time);
     timerRunning = false;
 }

Hope this helps!

Comment
Add comment · 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

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

223 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

Related Questions

Iterate through dialogue array and display in game 1 Answer

Array reference being changed without me knowing, or, When a coroutine is run on a monobehavior and then stopped with StopCoroutine (coroutine) it stops the same coroutine being run on another instance of the same monobehavior on a different object. 1 Answer

Assigning a List string element from script A to another string variable in scriptA from scriptB 1 Answer

can't save values from another class to a list 1 Answer

How do I view an array that is inside of an array in the unity inspector? 0 Answers


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