• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Xitech_ · Nov 26, 2013 at 05:36 PM · arrayarraylist

ArrayLists giving wrong information? C#

Hello there,

I am trying to make an Dialogue system(I dont want to use any plugins as this is a learning procces for me). But the ArrayLists are giving me other information then expected. This is my code:

 private ArrayList Questions = new ArrayList();
 private ArrayList Answers = new ArrayList();
 
   
     //public Texture CharacterTexture;
     void Start () {
       
         Answers.Add("Doorgaan?");
         Answers.Add("nee");
         Answers.Add("ja");
         Questions.Add(Answers);
         Answers.Clear();
         Answers.Add("En wat nu weer?");
         Answers.Add("aaaa");
         Answers.Add("bbbb");
         Questions.Add(Answers);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void OnGUI()
     {
         
         if(GUI.Button(new Rect(Screen.width / 2 - 150,250,300,50),((ArrayList)Questions[0])[0].ToString())){
             Debug.Log("Je hebt nu op nr 0 geklikt");
         }
         if (GUI.Button(new Rect(Screen.width / 2 - 150, 300, 300, 50), ((ArrayList)Questions[0])[1].ToString()))
         {
             Debug.Log("Je hebt nu op nr 1 geklikt");
         }
         if (GUI.Button(new Rect(Screen.width / 2 - 150, 350, 300, 50), ((ArrayList)Questions[0])[2].ToString()))
         {
             Debug.Log("Je hebt nu op nr 2 geklikt");
         }

Now I expect 'Questions[0])[0]' to display "Doorgaan?", 'Questions[0])[1]' "Nee" etc. But instead it is showing me "en wat nu weer?", "aaaa" and "bbbb".. How can I fix it? What am I doing wrong in my code?

Thank you for your time :)

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

2 Replies

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

Answer by yatagarasu · Nov 26, 2013 at 05:39 PM

You should allocate new Array, not Clear the old one.

  void Start () {
  
 Answers.Add("Doorgaan?");
 Answers.Add("nee");
 Answers.Add("ja");
 Questions.Add(Answers);
 
 Answers = new ArrayList();
 Answers.Add("En wat nu weer?");
 Answers.Add("aaaa");
 Answers.Add("bbbb");
 Questions.Add(Answers);
 }
Comment
Add comment · Show 2 · 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
avatar image Lovrenc · Nov 26, 2013 at 05:42 PM 0
Share

Since he looses the reference to first set of answers, this is effectively the same.

avatar image Xitech_ · Nov 26, 2013 at 06:59 PM 0
Share

I see what I did wrong, you're right. It's a reference to the Answers array, and I clear it, and simply put new data in it, that's why it showed the last answers I added. Thanks!

avatar image
1

Answer by Lovrenc · Nov 26, 2013 at 05:41 PM

Since this is a learning project i dont know how much of a "spoiler" answer you want.

However, your problem is in your data structure. You have one Answers list for all your questions. This wont work. You need a set of answers for every question.

So why do you get these results. You used

 Answers.Clear();

and cleared the list => you lost the answers for 1st question.

Comment
Add comment · Show 1 · 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
avatar image Xitech_ · Nov 26, 2013 at 07:00 PM 0
Share

I see, thanks, same as other answer :p

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

get int based on 3 ints 0 Answers

Help with for loop and arrays 3 Answers

How to acces the value of List of arrays 0 Answers

How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers

GameObject to Array/List or whatever fits best 0 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