Ok I’m terrible at coding, and have spent hours looking for a solution but can’t find anything that works.
I want to make a simple little cut-scene like an old computer loading where the dialogue comes up with the typewriter effect, stays there for a few seconds, then automatically moves onto the next string with the same effect.
First I tried this, which gave me the typing effect.
[TextArea(3,5)]
public string fullText;
//How long each character will take to appear.
public float delay;
private string currentText = "";
// Use this for initialization
void Start()
{
StartCoroutine(ShowText());
}
IEnumerator ShowText()
{
//Add a character after delay until it's reached the end of the full text.
for (int i = 0; i< fullText.Length; i++)
{
currentText = fullText.Substring(0, i);
this.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}
}
But since the actual string can’t be an array otherwise it just gets error about not having a definition about substring, i’m left with just one page of text to work with. I don’t want it to all write out at once, I want there to be a delay between strings. “searching…” [pause] “…” [pause] “No results.” sort of thing, with each new string replacing the last. I’ve tried looking it up but can’t find anything that works. Is there anyway to do that with one singular text box?
I tried a different method;
private Text _textComponent;
//Array of strings for multiple lines
public string[] DialogueStrings;
public float SecondsBetweenCharacters = 0.3f;
//check to see if it's reached end of string
private bool _isEndofDialogue = false;
// Use this for initialization
void Start () {
_textComponent = GetComponent<Text>();
//Start displaying text automatically on start.
StartCoroutine(DisplayString(DialogueStrings[0]));
}
// Update is called once per frame
void Update () {
}
private IEnumerator StartDialogue()
{
int dialogueLength = DialogueStrings.Length;
int currentDialogueIndex = 0;
while(currentDialogueIndex < dialogueLength || _isEndofDialogue)
{
if(_isEndofDialogue == true)
{
StartCoroutine(DisplayString(DialogueStrings[currentDialogueIndex++]));
}
}
yield return 0;
}
private IEnumerator DisplayString(string stringToDisplay)
{
int stringLength = stringToDisplay.Length;
int currentCharacterIndex = 0;
_textComponent.text = "";
while (currentCharacterIndex < stringLength)
{
_textComponent.text += stringToDisplay[currentCharacterIndex];
currentCharacterIndex++;
if (currentCharacterIndex < stringLength)
{
yield return new WaitForSeconds(SecondsBetweenCharacters);
}
else
{
yield return new WaitForSeconds(1f);
_isEndofDialogue = true;
break;
}
_textComponent.text = "";
}
}
}
Which is a poorly altered bit of code originally intended to move to next string after having the player press enter, but copying it word for word didn’t work at all, it just made the first string disappear and never show the next.
Sorry this is really lot, I’d appreciate any help a lot.