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 /
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.

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

Can anyone help me with a system that helps me to access data inside of data? 2 Answers

Using a list of arrays to make Unity UI 2D button toggle colors 0 Answers

My loop keeps producing the same wrong result :,,( 2 Answers

What do you think of this? List, Vector 3 and Update 1 Answer

Linear Interpolation of Array Values 1 Answer


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