- Home /
dialogue/text box list not working as intended
first of all, here's the code - so i have an interact button which if ui text box is visible- goes to change the text to whatever i set it to - and recently i wanted to create a list so i could add several texts at once, and for each click move on to the next stored text and remove the one just read - however, this doesnt seem to work as intended. Any input?
public void nextBodyText()
{
if (isCurrentlyVisible)
{
if (bodyTextList.Count > 0)
{
bool hasGrabbedText = false;
foreach (string tempString in bodyTextList)
{
Debug.Log(tempString);
//This isnt gonna work later on, we need to fix..
if (!hasGrabbedText) {
displayBodyText = tempString;
hasGrabbedText = true;
}
//displayBodyText +"\n"+ tempString;
}
bodyTextList.RemoveAt(1);
}
else
{
setBodyText("Nothing.");
nextBodyText();
}
}
else
{
Debug.Log("Dialogue Box is currently Hidden");
}
}
public bool checkIfVisisble()
{
bool tempVis;
tempVis = isCurrentlyVisible;
Debug.Log("Dialogue box is visible: "+tempVis);
return tempVis;
}
public void setDialogueBoxVisible()
{
isCurrentlyVisible = true;
}
public void setDialogueBoxHidden()
{
isCurrentlyVisible = false;
}
public void setBodyText(string tempText)
{
Debug.Log("Added to text list: "+tempText);
bodyTextList.Add(tempText);
//displayBodyText = tempText;
}
i included all the relevant methods. Just setting the text works perfectly, the problem arrives when i try to move on to the next text. Its as if it doesnt get added to the list, but as you can see, it clearly does.
Another thing is - it does actually show the FIRST and ONLY the first thing to be displayed- and then never again, does it display anything else, i.e if i put nothing and press interact, it displays "nothing" if i do a battle thing it says "Battle has started" - whatever is the first to be inputted is what it shows - and the list never seems to have anything other than that, added to it. Seems strange to me, im well versed in using lists in java for my objects, but this doesnt seem to work.
I would just use "first item in list" but it gives me an error, which is why i had to do a for each loop.
Answer by bpaynom · Sep 04, 2018 at 02:48 PM
You could replace
bool hasGrabbedText = false;
foreach (string tempString in bodyTextList)
{
Debug.Log(tempString);
//This isnt gonna work later on, we need to fix..
if (!hasGrabbedText) {
displayBodyText = tempString;
hasGrabbedText = true;
}
//displayBodyText +"\n"+ tempString;
}
bodyTextList.RemoveAt(1);
For this
if ( bodyTextList.Count > 0 ){
displayBodText = bodyTextList[0];
bodyTextList.RemoveAt(0);
}
What you were doing wrong was that you were removing the second element, instead of the first. An extraclarification from @Harinezumi :
All collections (arrays, List, Set, Queue, etc.) index starting with 0, so bodyTextList.RemoveAt(1) removes the second element in the array. You possibly didn't run into this, but when you only have 1 element left in your bodyTextList, calling RemoveAt(1) on it will through an indexing exception.
Yes, this should help, I just want to add a clarification: all collections (arrays, List, Set, Queue, etc.) index starting with 0, so bodyTextList.RemoveAt(1)
removes the second element in the array.
You possibly didn't run into this, but when you only have 1 element left in your bodyTextList
, calling RemoveAt(1)
on it will through an indexing exception.
I've edited my answer because, like you say, if you use RemoveAt(1);
and there is only one element, then it'll throw an exception. So, if there is no elements and you use RemoveAt(0);
the same exception will be thrown.
Hehehe, thanks- i didnt even see that, replaced the removeAt 1 to 0, fixed it. I dont know what i was thinking.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174033im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assign the name of string as name of predefined List 2 Answers
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Change alpha color of specific letter in a Text UI with C# 2 Answers