- Home /
arraylist doesn't stop adding to string-infinite c#
I'm trying to get some strings saved in an arraylist to show up on the GUI, but for some unkown reason to me, it won't stop after going through the arraylist and keeps going on for ever. i'm using the foreach, because i want each entry only to show up once.
there's nothing wrong with the code itself (i think), but it has something to do with the foreach at the bottom of the code.
Help really appreciated!
Main:
public static string Box1Contents;
public static string Box2Contents;
public static string PlayerContents;
public static ArrayList Box1Array = new ArrayList();
public static ArrayList PlayerArray = new ArrayList();
public static ArrayList Box2Array = new ArrayList();
Start:
void Start () {
Box1Array.Add("one");
Box1Array.Add("two");
Box1Array.Add("three");
PlayerArray.Add("six");
Box2Array.Add("four");
Box2Array.Add("five");
}
OnGUI:
if (GUI.Button(new Rect(460, 400, 140, 65), "Pick Up"))
{
switch (Player.triggeredObject)
{
case "Box1":
if (Box1Array.Count > 0)
{
PlayerArray.Add(Box1Array[Box1Array.Count - 1].ToString());
Box1Array.RemoveAt(Box1Array.Count - 1);
GameManager.Box1Contents = "";
GameManager.PlayerContents = "";
}
break;
case "Box2":
if (Box2Array.Count > 0)
{
PlayerArray.Add(Box2Array[Box2Array.Count - 1].ToString());
Box2Array.RemoveAt(Box2Array.Count - 1);
GameManager.Box2Contents = "";
GameManager.PlayerContents = "";
}
break;
default:
//nothing
break;
Also OnGUI:
foreach (object s in Box1Array)
{
Box1Contents = Box1Contents + s + "\n";
}
foreach (object t in PlayerArray)
{
PlayerContents = PlayerContents + t + "\n";
}
foreach (object u in Box2Array)
{
Box2Contents = Box2Contents + u + "\n";
}
Answer by DaveA · Mar 15, 2012 at 09:48 PM
The stuff in 'also OnGUI' will execute several times per frame. The simplest thing probably to set Box1Contents = ""; before that loop, and similar for the other 'Contents' strings.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Javascpt Array to c# Arraylist (shift and pop) 2 Answers