Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by GrafSpee · Oct 15, 2017 at 02:19 PM · listlist of lists

Problem when changing a value in the list

Hi Guys,

I'm having a problem with my list and what happen is that when I change a single value on the list, all the values int the list changes.

Down Below is my code to setup my list.

 void PreLoadBattleData()
 {
     //Preparing the needed List
     List<int> tempList = new List<int>(2);
     // 0 - 111 SquareNumber
     for (int i = 0; i < 112; i++)
     {
         tempList.Add(0); //Add 0 for occupant
         tempList.Add(mapData[i]); //Add terrain data
         battleData.Add(tempList); //Then integrate it to BattleData
     }
 }

Then after running some functions I will arrive with this method.

 void DeployUnit()
 {
     int unitSelected = battleManager.GetComponent<BattleManager>().popUpPage;
     battleManager.GetComponent<BattleManager>().battleData[int.Parse(gameObject.transform.parent.name)][0] = (unitSelected + 1);
 }

The last line should only change a single value in the list. In example battleData[50][0] the value from 0 will be changed to 1 but when I print them on the console.

All battleData[(from 0-111)][0] will print the value of 1 even though I only changed 1 part of it.

Please help, this seems basic but I could not find out where did I go wrong. I've tried debugging all the codes that will leave up to that method but only that method can change the values in my List.

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 GrafSpee · Oct 16, 2017 at 01:54 PM

Able to fixed it on my own. I use the same code when I change variables from a different script and figured that that script can't be incorrect. So I experimented with how I build the list. Below is what fixed the issue;

 void PreLoadBattleData()
 {
     for(int i = 0; i < 112; i++)
     {
         List<int> tempList = new List<int>(2);
         for (int x = 0; x < 2; x++)
         {
             tempList.Add(0);
         }
         battleData.Add(tempList);
     }
     
     for(int i = 0; i < 112; i++)
     {
         battleData[i][1] = mapData[i];
     }
 }

Although I am not sure how I am able to fix it because I don't see any difference aside from making the code more complicated. So I would appreciate it if someone can point it out for me.

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

118 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 2 Answers

reorderable list of reorderable list items? (Editor) 0 Answers

Nested lists; How to add/decalre lists at runtime? 0 Answers

Adding to a List inside a Coroutine doesn't add properly? 1 Answer

Destroy GameObject in list then instantiate GameObject not working 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