Adding a unique element from one list to another list
I have question to which I hope has a simple answer for a more skilled programmer than myself. I have created lets say two lists with many variables. List A and List B. I want to duplicate an item from List A to List B and I mean just that not make a reference of it an actual duplicate. So I can edit something on List B and not change the value of it on List A or change a duplicate item on List B. Help here would be greatly appreciated Thank You in advance.
Answer by TBruce · Apr 19, 2016 at 07:37 PM
I have modified my original answer using a slightly version of your Item class. I tested this using OnMouseDown() on a 3D Cube. This works using both methods AddItemToListB() listed. You can separate what you want into multiple files to make it fit your needs.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//////////All the information needed for the reels//////////
[System.Serializable]
public class Item
{
public string Name;
public float Weight;
public float Size;
public int Index;
public Item(string name, float weight, float size, int index)
{
Name = name;
Weight = weight;
Size = size;
Index = index;
}
}
public class MyClassA : MonoBehaviour
{
public List<Item> listItemA = new List<Item>();
public List<Item> listItemB = new List<Item>();
void Start()
{
// populating intListA[] so there is something in it
for (int i = 0; i < 10; i++)
{
listItemA.Add(new Item("Item " + i.ToString(), (float)UnityEngine.Random.Range(5, 160), (float)UnityEngine.Random.Range(2, 7), i));
}
}
void OnMouseDown()
{
int index = (int)UnityEngine.Random.Range(0, listItemA.Count);
AddItemToListB(listItemA[index]);
// or you can call like this
// AddItemToListB(listItemA[index].Name, listItemA[index].Weight, listItemA[index].Size, listItemA[index].Index);
}
void AddItemToListB(Item item)
{
Item newItem = new Item(item.Name, item.Weight, item.Size, item.Index);
newItem.Name = item.Name + " - Modified"; // this is to show you that you can modifiy a variabile in one list and it will not change in another
newItem.Index = (int)UnityEngine.Random.Range(100, 10000); // this is to show you that you can modifiy a variabile in one list and it will not change in another
listItemB.Add(newItem);
}
void AddItemToListB(string name, float weight, float size, int index)
{
Item newItem = new Item(name, weight, size, index);
newItem.Name = name + " - Modified"; // this is to show you that you can modifiy a variabile in one list and it will not change in another
newItem.Index = (int)UnityEngine.Random.Range(100, 10000); // this is to show you that you can modifiy a variabile in one list and it will not change in another
listItemB.Add(newItem);
}
}
Right I understood that is what I needed to do. I think I just phrased my question poorly and even so I believe on your answer its obvious there inst a more optimal way of adding a new unique item based on another which is what I was looking for in the short of it but I will implement something close to what you have demonstrated in my script. Thank you $$anonymous$$avina for your answer it was helpful.
Answer by Zivisx · Apr 19, 2016 at 09:19 PM
So I have a custom list made in another class called Item. So what you are saying is I basically need to create a Item variable and call this new item to add to my BList. My follow up question would be is there an easier way to copy this information than listing every single variable and filling it out manually I just figured I was missing some simple wording that would do this for me.
Item tempItem = new Item (ListA[0].Name, ListA[0].Weight, ListA[0].Size......so on); something more like this item tempItem = new Item (Copy LIstA[0]);
ListB.add (tempItem);