- Home /
Only want to change one item in list and not all of it's duplicates?
Hi guys, I have a question involving lists and object pools. I am not sure how to best go about fixing this problem.
So, I have an object pool where I create a list of items. When the game starts, these items form into a list of buttons. When a button is clicked on, the item is duplicated and sent to the other list of buttons, and the date it is added to the list is written on the button. The issue is, if I click the same button twice, then all of the times automatically change to the same value. Here are some screenshots to show what i mean: When my first item, "Go to Work", is added to my list in game, the time displays correctly:
But, when I add another "Go to Work" button to my list, both times reset to become the same time! Here is what happens:
By the way, in the images shown, this is the list that I am adding buttons to, from another list. What I want is for the time on one to say 14:55 and the time on the other to say 14:57... but they both change to 14:57 automatically. This doesn't happen when I select a button to add that has a different name, but only when they are the same starting out.
Here my code dealing with the object pool, items, and buttons:
Code where the buttons are created:
[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 class ActivityScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ActivityScrollList otherShop;
public SimpleObjectPool buttonObjectPool;
private void AddItem(Item itemToAdd, ActivityScrollList activityList){
activityList.itemList.Add (itemToAdd);
}
private void AddButtons()
{
for (int i = 0; i < itemList.Count; i++) {
Item item = itemList [i];
GameObject newButton = buttonObjectPool.GetObject ();
newButton.transform.SetParent (contentPanel);
ActivityBookButton activityButton = newButton.GetComponent<ActivityBookButton> ();
activityButton.Setup (item, this);
activityButton.SetupDate (item, this);
}
}
Code where items are assigned to elements of my buttons:
public class ActivityBookButton : MonoBehaviour {
public Button button;
public Text nameLabel;
public Image iconImage;
public Text prodValue;
public Text socValue;
public Text healthValue;
public Text funValue;
public Image checkMark;
public Text timeAdded;
public Item item;
private ActivityScrollList scrollList;
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;
}
public void SetupDate(Item currentItem, ActivityScrollList currentScrolllist)
{
item = currentItem;
timeAdded.text = item.timeAdded;
scrollList = currentScrolllist;
}
Also if you want to see the code for my object pool, I just used the object pool code from the tutorial here: https://unity3d.com/learn/tutorials/topics/user-interface-ui/scroll-view I used the code for "SimpleObjectPool" at the bottom of the page. I know this is a lengthy post so thanks a million to anyone who even suggests any ideas, but I just was hoping for some outside opinions on how to go about this. Thanks for any help or responses in advance!
Answer by Xavier78 · May 08, 2018 at 03:38 AM
Well it looks like you are doing a shallow copy, and not a deep one(passing a reference around, vs makeing a new object, and copying the values over). So either in SetupDate or setup don't do item = currentItem. but either do a .clone or look into how to do a deep copy of a reference object.
Thank you! How would I go about using a deep copy ins$$anonymous$$d of a shallow copy? Would I do that in my Setup function or somewhere else?