- Home /
Dialog system - Spacebar continue
Hey everyone I got an issue with my code. I'm quite new to this so please don't blame me, but i'm trying to make a dialog system for my game but just can't seems to get it right. The first sentence shows up but it doesn't go to the next sentence. Anyone have an answer for this??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Dialog : MonoBehaviour
{
public TextMeshProUGUI textDisplay;
public string[] sentences;
private int index;
public float typingSpeed;
void Start()
{
StartCoroutine(Type());
}
IEnumerator Type()
{
foreach(char letter in sentences[index].ToCharArray())
{
textDisplay.text += letter;
yield return new WaitForSeconds(0.02f);
}
}
public void NextSentence()
{
if (Input.GetKeyDown(KeyCode.Space))
{
index++;
textDisplay.text = "";
StartCoroutine(Type());
}
}
}
Answer by Klarzahs · Oct 13, 2020 at 10:18 AM
Hi @BIGSIPOFCOFFEE ,
your problem is that NextSentence() never gets called, ergo the if statement will never be checked.
You would have to call it the following way, as update() gets called every frame from unity:
public void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
NextSentence();
}
}
and
public void NextSentence(){
index++;
textDisplay.text="";
StartCoroutine(Type());
}
Please be aware that fast space pressing would lead to multiple coroutines writing over each other.
Standard browser disclaimer, so be wary of typos etc
Hope this helps
Answer by BIGSIPOFCOFFEE · Oct 14, 2020 at 01:59 PM
Holy smokes it works, thank you so much! Been trying to figure it out for days. Really can't thank you enough <3
Your answer

Follow this Question
Related Questions
Advice for making a 2D Dialogue System? 2 Answers
Accessing local system ( File Browser ) 2 Answers
DIY Dialog System (type-writer effect) 1 Answer
Creating a Quest system 2 Answers
On click add listener not working ! 0 Answers