I keep getting this error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Dialog.ShowDialog (System.Int32 dialogStartLine) (at Assets/Scripts/Dialog.cs:77)
Dialog.Interacted () (at Assets/Scripts/Dialog.cs:31)
PlayerInteraction.OnTriggerStay2D (UnityEngine.Collider2D col) (at Assets/Scripts/Player/PlayerInteraction.cs:24)
With this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Dialogs
{
public string name;
public string dialog;
public bool stopHere = false;
public List<FuncCall> fc; //Calls the function afterwards
public Sprite profileImage;
}
public class Dialog : PlayerInteractable
{
public List<Dialogs> dialog;
public GameObject dialogBox;
public bool dialogOnInteract = true;
public int dialogLine = 0;
//private bool dialogDelay;
private Text nameText;
private Text dialogText;
public override void Interacted()
{
if(dialogOnInteract)
{
ShowDialog(dialogLine);
}
}
public void ShowDialog(int dialogStartLine)
{
dialogBox.SetActive(true);
int i = dialogStartLine;
while(i < dialog.Count - 1 || !dialog[i].stopHere)
{
if(i > 0)
{
if(dialog[i].name != dialog[i - 1].name)
{
StartCoroutine(TypeDialogName(dialog[i].name));
}
}
else
{
StartCoroutine(TypeDialogName(dialog[i].name));
}
//dialogDelay = true;
StartCoroutine(TypeDialog(dialog[i].dialog));
//while(dialogDelay) // || DialogTyping)
//{
//}
while(!(Input.GetAxis("Jump") >= 0.1f) && !(Input.GetAxis("Interact") >= 0.1f))
{
}
/*
if(dialog[i].stopHere)
{
//break;
}
*/
i++;
}
dialogBox.SetActive(false);
}
IEnumerator TypeDialogName(string name)
{
nameText = dialogBox.transform.GetChild(0).GetComponent<Text>();
nameText.text = "";
foreach(char character in name.ToCharArray())
{
nameText.text += character;
yield return new WaitForSeconds(0.05f);
}
/*
for(int i = 0; i < name.Length; i++)
{
nameText.text += name[i];
yield return new WaitForSeconds(0.1f);
}
*/
}
IEnumerator TypeDialog(string myDialog)
{
dialogText = dialogBox.transform.GetChild(1).GetComponent<Text>();
dialogText.text = "";
foreach(char character in myDialog.ToCharArray())
{
dialogText.text += character;
yield return new WaitForSeconds(0.02f);
}
//dialogDelay = false;
}
}
It’s supposed to show dialog like a typewriter with the option to call a function after displaying text and it works if I unpause in the unity Editor.
Any Ideas?
Thanks in advance.