Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NGKrush1 · Mar 15, 2012 at 08:48 PM · c#foreacharraylist

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";
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Javascpt Array to c# Arraylist (shift and pop) 2 Answers

Issue accessing a GameObject through an Arraylist 2 Answers

C# script makes unity freeze on ArrayList.Add() 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges