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 /
avatar image
0
Question by DeadKenny · Mar 11, 2018 at 02:39 PM · arraylistlistsfor-loopforeach

Copy values between two classes in two lists.

I need to copy values and lists within the same class of two different lists. Essentially I just want to replace values like say population of classes in list B with that of list A. Also lists within the lists.

I know how to code foreach loops and stuff but want to know the method for this specific issue I have. I have tried many ways but can't figure it out.

eg:`

//Just imagine these are filled with stuff. List listA; List listB;

//There are also lists within the lists above... I need them to transfer too eg:

for loop(){

ListA[i].list = ListB[i].list;

}

`

Now what I need is to transfer stuff from ListA to ListB. But they have to fit the count/position so data gets transferred to the right one.

I am trying foreach and for loops... but I confuse myself further.

How can I do this correctly?

Comment
Add comment · Show 1
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 KittenSnipes · Mar 11, 2018 at 04:34 PM 0
Share

@Dead$$anonymous$$enny

Well if these lists are the same size that makes it very easy. If these lists have different sizes well then errors await you. Is the other list accessible due to a static classifier of the list or is it through a public classifier of the class itself?

Public Classifier Of Class Example": public class $$anonymous$$yClass : $$anonymous$$onoBehaviour //Class to reference //This is the public variable referenced to change the other list: public $$anonymous$$yClass myList;

Static Classifier Of List Example: public class $$anonymous$$yClass : $$anonymous$$onoBehaviour public static List myList; //Variable to reference

//How it looks in the other class: $$anonymous$$yClass.myList;

//Just matters how you are using the lists

1 Reply

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

Answer by KittenSnipes · Mar 11, 2018 at 04:50 PM

@DeadKenny

Try using these as an example. I think it should help a bit but I did not implement any null checks so make sure to do so when using Lists. I think you can figure it out.

Example Class 1:

 public class Zeros : MonoBehaviour {
     public static List<int> ZerosList;

     void Start()
     {
         ZerosList = new List<int>();
         for (int i = 0; i < 10; i++)
         {
             int random = Random.Range(0, 2);
             ZerosList.Add(random);
         }
     }
 
     // Update is called once per frame
     void Update () {
         for (int i = 0; i < ZerosList.Count; i++)
         {
             if (ZerosList[i] == 1)
             {
                 ZerosList.Remove(ZerosList[i]);
                 Ones.OnesList.Add(ZerosList[i]);
             }
         }
         PrintList(ZerosList);
     }
 
     void PrintList(List<int> listToPrint)
     {
         Debug.Log("Zeros: ");
         for (int i = 0; i < listToPrint.Count; i++)
         {
             Debug.Log("Array Element" + i + ": " + listToPrint[i]);
         }
     }
 }

Example Class 2:

 public class Ones : MonoBehaviour {
     public static List<int> OnesList;

     void Start () {
         OnesList = new List<int>();
         for (int i = 0; i < 10; i++)
         {
             int random = Random.Range(0, 2);
             OnesList.Add(random);
         }
     }
 
     void Update () {
         for (int i = 0; i < OnesList.Count; i++)
         {
             if (OnesList[i] == 0)
             {
                 OnesList.Remove(OnesList[i]);
                 Zeros.ZerosList.Add(OnesList[i]);
             }
         }
         PrintList(OnesList);
     }
 
     void PrintList(List<int> listToPrint)
     {
         Debug.Log("Ones: ");
         for (int i = 0; i < listToPrint.Count; i++)
         {
             Debug.Log("Array Element" + i + ": " + listToPrint[i]);
         }
     }
 }

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 DeadKenny · Mar 12, 2018 at 02:27 AM 0
Share

Thanks, even though the real issue was co$$anonymous$$g from another part/issue of the script, this is still help full for data transfer part.

Thanks again. :P

avatar image KittenSnipes DeadKenny · Mar 12, 2018 at 02:28 AM 0
Share

No problem glad I could at least do something

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

86 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

Related Questions

Affect every object in array. 1 Answer

Cache materials into List (or array) and restore from cached List? 0 Answers

How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers

Assign role randomly from array 2 Answers

Is there a way to remove array entries in the editor? 4 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