- Home /
My string list is empty
I have this list
//TODO: Put these lists in a separate method
public static List<string> task1Gesture = new List<string>(new[]
{
"Tap",
"Grab",
"Grip"
});
And this other list which will store the contents of the first list: public static List selection = new List();
When the user presses a button this happens:
if (drawButon(btn_Next, "Next"))
{
if (Cube_DemoPhase.task1Gesture.Count > 0)
{
Cube_DemoPhase.selection.Add(Cube_DemoPhase.task1Gesture[Cube_DemoPhase.randomIndex_task1]);
Cube_DemoPhase.task1Gesture.RemoveAt(Cube_DemoPhase.randomIndex_task1);
if (Cube_DemoPhase.task1Gesture.Count == 0)
{
//TODO: Logic for next task goes here
return;
}
Cube_DemoPhase.randomIndex_task1 = Random.Range(0, Cube_DemoPhase.task1Gesture.Count - 1);
}
}
Then I am outputting my list information into this string array here:
string[] gestureOptions = { Cube_DemoPhase.selection.ConvertToString(),
Cube_DemoPhase.selection.ConvertToString(),
Cube_DemoPhase.selection.ConvertToString()
};
Then I call this array into the OnGUI() method
But for some reason my list shows up empty. Can anyone help me figure out the reason behind this? Thank you very much!!!
I believe I tried helping you with a very similar question that you posted here yesterday.
@$$anonymous$$ey_Less you did indeed help me. But for some reason I am still getting an empty list. The thing is that I kept a debug log to see if it is adding anything when the user clicks on the button and it is indeed adding them. But when I cal that same list from another class the list seems to be empty.
We need more detail than that. Which list is empty exactly -- gestureOptions, Cube_DemoPhase.selection, task1Gesture? And how do you deter$$anonymous$$e it is "empty"? And what's the code from the other class that sees it as empty?
@$$anonymous$$TheDev I figured out why the list is empty and now it id not. I dug up this piece of code that may help me but what it is doing is printing random elements from my list non stop, but I want to print the order in which the random number generator had picked it during gameplay.
for (int i = gestureOptions.Length - 1; i > 0; i--)
{
var r = Random.Range(0, i);
var tmp = gestureOptions[i];
gestureOptions[i] = gestureOptions[r];
gestureOptions[r] = tmp;
drawSelectionGrid(selGrid_GestureOptions, gestureOptions, ref gestureIndex_Block_Task1, 3);
You have any idea on how to get this to print how I want it? I know I am close to figuring this out with that code snippet, looks "hacky" I know and I will figure out a better solution than that but right now this is what I have to work on. Hope this diagram helps. And thank you, friend.
It's never going to print out in the same order that you previously generated unless you use those values. I already gave you the solution in your other question's thread.
Answer by mmangual_83 · Jan 07, 2014 at 02:12 PM
I figured it out:
//declare in my globals
string[] gestureOptions_task1 = Cube_DemoPhase.task1Gesture.ToArray();
//This goes into the update method
var gestures = new string[gestureOptions_task1.Length];
for (var i = 0; i < gestures.Length; i++)
{
gestures[i] = Cube_DemoPhase.index_task1[i];
}
I also made sure that the second list where I was saving the data of the first list was also of the same variable type. So in my case I needed both lists to be strings.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
C# Displaying List Elements in Multiples 1 Answer
Printing a GUI selection grid in order 1 Answer
Printing an ordered list 1 Answer