Named array assigned to new array, named array returns null on all subsequent calls
Disclaimer: I am new to Unity. I may be missing something simple.
For my dialogue system, when the player interacts with one game object, it reassigns the text for that object stored in the system as a string[] to a new string[], stored within the game object interacted with. This works fine if you only interact once. Once the text has to be reassigned a second time, the array starts returning null. Here's the relevant code: if (Input.GetKeyDown("z")) {dialoguepanel.GetComponentInChildren<dialoguesys>().dtxt = new string[] { "(Oh boy, what flavour?)" };
dtxt is the array that's been giving me trouble. If there's something I'm missing about array assignment, I'd love to hear it.
what does dialoguesys do? can you post the code that references dtxt, or is it some asset store code?
Dialoguesys is a script that allows the player to click on the UI containing the dialogue, cycle through the dialogue, and exit the panel or ask a player a question as needed. dtxt is a public string[] with a default setting in the inspector of length 2. The only reference to dtxt in the script that's been causing issues in game is the one pasted above, if that saves you time reading through the code below. I added some comments to make it a bit more legible. Sorry about the length, I figured I might as well just be thorough.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class dialoguesys : $$anonymous$$onoBehaviour, IPointerClickHandler
{
public bool question;
public GameObject container;
public GameObject qcontainer;
public Text dialogue;
public string[] dtxt;
public Image talkspr;
public Sprite[] dimg;
private int conlength;
private int i;
private int i1;
void OnEnable()
{
conlength = dtxt.Length;
Debug.Log(conlength);
if (!qcontainer.activeSelf) { Debug.Log("not using given"); }
//checks if a question has been answered, and replaces the text with the selected response
if (qcontainer.activeSelf)
{
Debug.Log("using given");
//holder is a script holding the responses.
dtxt = qcontainer.gameObject.GetComponentInChildren<holder>().text;
dimg = qcontainer.gameObject.GetComponentInChildren<holder>().image;
conlength = dtxt.Length;
Debug.Log(conlength);
}
i = 1;
i1 = 0;
dialogue.text = dtxt[0];
talkspr.sprite = dimg[0];
}
public void OnPointerClick(PointerEventData GUIclick)
{
Debug.Log("i have been clicked.");
if (conlength > 1)
{
dialogue.text = dtxt[i];
talkspr.sprite = dimg[i];
}
i1++;
if (i < conlength - 1) { i++; }
Debug.Log(i1);
Debug.Log(conlength);
//at the end of a conversation, asks a question, or closes the panel.
if (i1 == conlength)
{
if (question == true)
{ qcontainer.SetActive(true); }
container.SetActive(false); }
}
}
I hope this helps.