- Home /
Linking together my dialogue scripts with typewriter effect script
Hello Unity! I have made several dialogue scripts which display multiple lines of text when I interact with objects that have some of the dialogue scripts on them. Then, I tried to give them the typewriter effect, which makes the letters appear one by one. How do I implement this typewriter effect script into my other scripts? Here are the scripts. using UnityEngine; using System.Collections; using UnityEngine.UI;
public class TypeWriterEffect : MonoBehaviour {
public string[] dialogueLines;
public float delay = 0.1f;
public string fullText;
private string currentText = "";
// Use this for initialization
void Start () {
StartCoroutine(ShowText());
}
IEnumerator ShowText(){
for(int i = 0; i < dialogueLines.Length; i++){
currentText = dialogueLines.totring(0,i);
this.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}
}
This next script is on the object which I interact with to make the text appear. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DialogueHolder : MonoBehaviour {
public string dialogue;
public string[] dialogueLines;
private DialogueManager dMAn;
// Use this for initialization
void Start () {
dMAn = FindObjectOfType<DialogueManager>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "PlaceHolder")
{
if(Input.GetKeyUp(KeyCode.Z))
{
//dMAn.ShowBox(dialogue);
if(!dMAn.dialogueActive)
{
dMAn.dialogLines = dialogueLines;
dMAn.currentLine = 0;
dMAn.ShowDialogue ();
}
}
}
}
} This is the script with goes on my dialogue box. It allows me to display multiple lines of text. It also stops me from moving while the text box is up. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DialogueManager : MonoBehaviour {
public GameObject textBox;
public Text Text;
public bool dialogueActive;
public string[] dialogLines;
public int currentLine;
private PlayerController thePlayer;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
}
// Update is called once per frame
void Update () {
if (dialogueActive && Input.GetKeyUp (KeyCode.Z))
{
//textBox.SetActive (false);
//dialogueActive = false;
currentLine++;
}
if (!dialogueActive) {
textBox.SetActive (false);
}
if (currentLine >= dialogLines.Length) {
textBox.SetActive (false);
dialogueActive = false;
currentLine = 0;
thePlayer.canMove = true;
}
Text.text = dialogLines [currentLine];
}
public void ShowBox(string dialogue)
{
dialogueActive = true;
textBox.SetActive(true);
Text.text = dialogue;
} public void ShowDialogue() { dialogueActive = true; textBox.SetActive (true); thePlayer.canMove = false; } }