Hi everyone, so as of right now I am making a shallow copy of an item in a list. When I click on this item in game, a copy is made of it and that copy is put in another list. The problem is, when I click on this item more than once, I want each individual copy of this original item to still have different elements. Since a shallow copy is being made right now, each clone is exactly the same… which is not what I want. I am pretty sure the way to do this is to create a deep copy instead, but I don’t know how to change my code to be a deep copy.
To be more specific, I want the time I create each copied item to be different on each one. So when the first time the item is copied, the time on copy1 is 1:00. The second time the item is copied, the time on copy2 is 1:05. That sort of thing. Here is my code below:
[System.Serializable]
public class Item
{
public string activityName;
public Sprite icon;
public float Productivity = 1f;
public float Social = 1f;
public float Health = 1f;
public float Fun = 1f;
public string dateButtonAdded;
public string timeAdded;
public Sprite checkMark;
}
public void Setup(Item currentItem, ActivityScrollList currentScrolllist){
item = currentItem;
nameLabel.text = item.activityName;
iconImage.sprite = item.icon;
prodValue.text = item.Productivity.ToString ();
socValue.text = item.Social.ToString ();
healthValue.text = item.Health.ToString ();
funValue.text = item.Fun.ToString ();
checkMark.sprite = item.checkMark;
scrollList = currentScrolllist;
}
I create this items through a simple object pool. I got the code for this from a Unity tutorial. Here is the code for the object pool:
// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances of
public GameObject prefab;
// collection of currently inactive instances of the prefab
private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefab
public GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return that
if (inactiveInstances.Count > 0)
{
// remove the instance from the collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instance
else
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instance
return spawnedGameObject;
}
// Return an instance of the prefab to the pool
public void ReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the pool
if(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy it
else
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
So basically I am unsure of how to change my code to be able to create a deep copy instead. Any help on how to do this is greatly appreciated.