- Home /
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?
@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
Answer by KittenSnipes · Mar 11, 2018 at 04:50 PM
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]);
}
}
}
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
No problem glad I could at least do something
Your answer
Follow this Question
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