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 jcfrizz · May 07, 2018 at 09:39 PM · uilistsduplicateitemsobject pool

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: alt text

But, when I add another "Go to Work" button to my list, both times reset to become the same time! Here is what happens: alt text

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!

two-items.png (250.1 kB)
one-item.png (151.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 jcfrizz · May 08, 2018 at 08:57 PM 0
Share

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?

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

139 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 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

Is there a way to use the prefab image unity shows? 0 Answers

Deleting button when item removed from list? 1 Answer

Adding object to list add the same object to another list 1 Answer

Why does the script don't apply to the other duplicated player 0 Answers

I'm trying to make a list driven menu for items, skills that meet a interactable interface like the one in Persona 3 Portable, can someone help? 0 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