Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Fixed issue don't post pls

Discussion in 'UGUI & TextMesh Pro' started by yusanuh, May 18, 2023.

  1. yusanuh

    yusanuh

    Joined:
    Mar 9, 2020
    Posts:
    1
    Hey everyone, I was watching this video for a dialogue system. I followed every step my code was right but I get an error unlike others.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class Dialogue : MonoBehaviour
    {
    public TextMeshProUGUI textComponent;
    public string[] lines;
    public float textSpeed;

    private int index=0;

    // Start is called before the first frame update
    void Start()
    {
    textComponent.text = string.Empty;
    StartDialogue();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey(KeyCode.Space))
    {
    if (textComponent.text == lines[index])
    {
    NextLine();
    }
    else
    {
    StopAllCoroutines();
    textComponent.text = lines[index];
    }
    }
    }

    void StartDialogue()
    {
    index = 0;
    StartCoroutine(TypeLine());
    }

    IEnumerator TypeLine()
    {
    foreach (char c in lines[index].ToCharArray())
    {
    textComponent.text += c;
    yield return new WaitForSeconds(textSpeed);
    }
    }

    void NextLine()
    {
    if (index < lines.Length - 1)
    {
    index++;
    textComponent.text = string.Empty;
    StartCoroutine(TypeLine());
    }
    else
    {
    gameObject.SetActive(false);
    }
    }

    }
    This is the code, and these are the error im receiving from Unity.

    IndexOutOfRangeException: Index was outside the bounds of the array.
    Dialogue+<TypeLine>d__7.MoveNext () (at Assets/Scripts/Dialogue.cs:46)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <c399efc08928422d8828d74a2b19ffba>:0)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    Dialogue:StartDialogue() (at Assets/Scripts/Dialogue.cs:41)
    Dialogue:Start() (at Assets/Scripts/Dialogue.cs:18)



    IndexOutOfRangeException: Index was outside the bounds of the array.
    Dialogue.Update () (at Assets/Scripts/Dialogue.cs:26)

    I'd like to hear some solutions from you.