Search Unity

Need Some Help With My Dialogue System

Discussion in 'Scripting' started by DeanAseraf1, Apr 16, 2019.

  1. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    Hey guys I'm new with scripting in unity and i have a problem with the dialogue System I have been working on
    the problem is in the Dialogue script according to unity


    After the third sentence [when the CurrentLine variable = 3 and the WhereToSwitchPosition variable = 3] I'm getting an error that the "Array index is out of range.".
    What does it mean and what can i do to fix this problem?


    Dialogue Manager Script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. public class DialogeManager : MonoBehaviour {
    7.     public GameObject DialogeBox;
    8.     public Text Text;
    9.     public Text Continue;
    10.     public Text TXTName;
    11.     public bool DialogeActive;
    12.     public string[] DialogeLines;
    13.     public string Name;
    14.     public int CurrentLine = 0;
    15.     // Use this for initialization
    16.     void Start () {
    17.         DialogeBox.SetActive(false);
    18.         DialogeBox.SetActive(false);
    19.         CurrentLine = 0;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (DialogeActive && Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             CurrentLine++;
    27.         }
    28.         if(CurrentLine>=DialogeLines.Length)
    29.         {
    30.             DialogeBox.SetActive(false);
    31.             DialogeActive = false;
    32.             CurrentLine = 0;
    33.         }
    34.         Text.text = DialogeLines[CurrentLine];
    35.         TXTName.text = Name;
    36.     }
    37.     public void ShowDialoge()
    38.     {
    39.         DialogeBox.SetActive(true);
    40.         DialogeActive = true;
    41.     }
    42. }
    43.  


    Dialogue Script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Dialoge : MonoBehaviour{
    6.     private DialogeManager DialogeManger;
    7.     public int CurrentDialogeMember = 0;
    8.     public int WhenToSwitchPosition = 0;
    9.     public string[] Names;
    10.     public int []WhereToSwitch;
    11.     [TextArea(1, 10)]
    12.     public string[] DialogeLines;
    13.     public bool HaveSpoken = false;
    14.     void Start()
    15.     {
    16.         DialogeManger = GameObject.FindObjectOfType<DialogeManager>();
    17.     }
    18.     void Update()
    19.     {
    20. if (DialogeManger.CurrentLine >= WhereToSwitch[WhenToSwitchPosition])
    21.         {
    22.             CurrentDialogeMember++;
    23.             WhenToSwitchPosition++;
    24.             DialogeManger.Name = Names[CurrentDialogeMember];
    25.             DialogeManger.DialogeLines = DialogeLines;
    26.         }
    27.         else
    28.         {
    29.             DialogeManger.Name = Names[CurrentDialogeMember];
    30.             DialogeManger.DialogeLines = DialogeLines;
    31.         }
    32.     }
    33.     public void SendDialoge()
    34.     {
    35.         if (DialogeManger.DialogeActive == false)
    36.         {
    37.             if (HaveSpoken == false)
    38.             {
    39.                 DialogeManger.ShowDialoge();
    40.                 HaveSpoken = true;
    41.             }
    42.         }
    43.     }
    44. }
    45.  

    DialogueTrigger Script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class CollisionsDialogeTrigger : MonoBehaviour {
    6.     public Dialoge DialogeToTrigger;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16.     private void OnTriggerEnter2D(Collider2D collision)
    17.     {
    18.         if (collision.gameObject.name == "Camille")
    19.         {
    20.             DialogeToTrigger.SendDialoge();
    21.         }
    22.     }
    23. }
    24.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    ArrayIndexOutOfRange means you're trying to access an array element that doesn't exist.

    Code (csharp):
    1.  
    2. int[] values = new int[10]; // 10 elements
    3.  
    4. values[67812983]; // error
    5.  
    So at some point, you're trying to access a dialogue or line of dialogue that doesn't exist.