Hi guys i have this code which connects a button to some text. It moves sentences along with a button click. I’d like to amend to move the sentence along with a key press but every time i put it in the update function it jumbles the text. Any suggestions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Dialog_New : MonoBehaviour
{
//----------------------------------------------------------------------
// Private variables (these variables are not accesible outside of this script)
private int index;
private bool Key_Enabled;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Public variables (these variables are accesible outside of this script)
// Pull through the player information to the prefab
public TextMeshProUGUI textDisplay;
public string[] sentences;
public float typingSpeed;
public GameObject continueButton;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public void NextSentence()
{
continueButton.SetActive(false);
if (index < sentences.Length - 1)
{
index++;
textDisplay.text = "";
StartCoroutine(Type());
}
else
{
textDisplay.text = "";
continueButton.SetActive(false);
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Coroutines run as functions but the code in it doesn't
// need to be run immediately
IEnumerator Type()
{
yield return new WaitForSeconds(2f);
// All code after the above will be delayed by two seconds
foreach (char letter in sentences[index].ToCharArray())
{
textDisplay.text += letter;
yield return new WaitForSeconds(typingSpeed);
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Start is called before the first frame update
void Start()
{
StartCoroutine(First_Meeting());
StartCoroutine(Type());
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.C))
{
Debug.Log("Hello");
NextSentence();
}
if (textDisplay.text == sentences[index])
{
StartCoroutine(SomeRoutine());
continueButton.SetActive(true);
}
}
//----------------------------------------------------------------------
IEnumerator First_Meeting()
{
sentences[0] = "Where am I?";
sentences[1] = "Oh..";
yield return new WaitForSeconds(0);
}
}