Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TeleMine · Oct 19, 2020 at 09:34 AM · coroutinecoroutine errors

coroutine doesn't work the second time

when i run my Game, the first Time i talk to my npc my code works normal. but when i try to talk to him a second time, it skips the dialogue. Can anyone help me?

My code: using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI;

 namespace project
 {
     public class NPC : MonoBehaviour
     {
 //checks if the player talks the first Time with the npc
         public bool hasSpoken = false;
 //The dialogue stuff
         public GameObject dialogBox;
         public Text dialogText;
 //a hint, wich says you wich button to press
         public Text hintText;
         public string hint;
 //the name of the npc
         public Text NPCname;
         public string NameText;
 //checks, if the player is near the npc
         public bool PlayerInRange;
 //more stuff for the hint
         public GameObject hintObject;
 //movment script
         public PlayerMovment movment;
 //wich text to show
         private string dialog;
 //wich quest the player is working on
         public int currentBook;
 //wich choice the player made in the quest
         public int choiceMade;
 //quest script
         public book book;
         
         
 
         private void Start()
         {
             movment.AllowedToMove = true;
             
             NPCname.text = NameText;
             PlayerInRange = false;
             choiceMade = 0;
         }
 
         private void Update()
         {
             if (PlayerInRange && Input.GetKeyDown(KeyCode.Space))
             {
                 
                 dialogBox.SetActive(true);
                 hintObject.SetActive(false);
                 movment.AllowedToMove = false;
 
                 StartCoroutine(DialogueSystem());
                 
             }
         }
 
         public void OnTriggerEnter2D(Collider2D collision)
         {
 
             hintObject.SetActive(true);
             hintText.text = hint;
             PlayerInRange = true;
             
         }
 
         public void OnTriggerExit2D(Collider2D collision)
         {
             hintObject.SetActive(false);
             PlayerInRange = false;
         }
 //dialogue
        IEnumerator DialogueSystem()
         {
             if (!hasSpoken)
             {
                 yield return waitForKeyPress(KeyCode.Space);
                 NextSentence("(dialogue).");
                 yield return waitForKeyPress(KeyCode.Space);
                 NextSentence("(dialogue)");
                 yield return waitForKeyPress(KeyCode.Space);
                 dialogBox.SetActive(false);
                 hintObject.SetActive(true);
                 movment.AllowedToMove = true;
                 hasSpoken = true;
                 currentBook = 1;
                 yield return null;
             }
             else
             {
                 if(currentBook == 1)
                 {
                     if (choiceMade == 0)
                     {
 //this part doesn't work
                         yield return waitForKeyPress(KeyCode.Space);
                         NextSentence("(Dialogue)");
                         yield return waitForKeyPress(KeyCode.Space);
                         dialogBox.SetActive(false);
                         hintObject.SetActive(true);
                         movment.AllowedToMove = true;
                     
                     }
                 }
             }
 
         }
 //shows the next sentence
         public void NextSentence(string sentence)
         {
             
             sentence = sentence.Replace("@", System.Environment.NewLine);
             dialogText.text = sentence;
             
         }
 
 //waits for the key to press
         public IEnumerator waitForKeyPress(KeyCode key)
         {
             
             bool done = false;
             
             while (!done) 
             {
                 if (Input.GetKeyDown(key))
                 {
                     done = true; 
                 }
                 yield return null;
             }
 
             
         }
 
 
 
     }
 }
 
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

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.

Follow this Question

Answers Answers and Comments

219 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

Related Questions

Coroutine does not stop? 1 Answer

Add points every five seconds 1 Answer

Coroutine cancels for no reason 0 Answers

Coroutine not working on second call 1 Answer

Using a coroutine to increment a for loop? 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