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
Your answer
![](https://koobas.hobune.stream/wayback/20220613014045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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