Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by AbleMikeCharlie · Feb 03 at 03:54 PM · instantiatelistscriptableobject

C# Add Unique Listener to Buttons Instantiated from List

I have a list of instantiated buttons. On button click for each, I want to 1) update a scriptable object boolean, and 2) move the instantiated button from one list to the other (add/remove). However, I am not able to uniquely identify the buttons after instantiation. - I have a master array of scriptable objects (rpg items). - I loop through the items and add each to the appropriate list (storage or equipped). - For each item, I instantiate a button in the appropriate scrollrect. This (above) all works. Where it breaks down is on adding a listener to the instantiated buttons. In the StoreItem() and EquipItem() functions, I am not able to access the unique scriptable objects in order to update the boolean and move from one list to another. Rather, I am only able to add generic functions, such as the Debug.Log that you see here. Any thoughts on how to adjust?

 // THE SCRIPTABLE OBJECT
 using UnityEngine;
 
 [CreateAssetMenu(fileName = "Item")]
 public class InventoryItemModule : ScriptableObject
 {
     public string ItemName;
     public bool isStorage;
     public bool isEquippped;
     public Sprite ItemImage;
 }
 
 // THE SCRIPT
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Inventory : MonoBehaviour
 {
 public InventoryItemModule[] masterInventoryItems;
     public GameObject inventoryPanel;
     public GameObject button;
     public List<InventoryItemModule> storageInventoryItems = new List<InventoryItemModule>();
     public List<InventoryItemModule> equippedInventoryItems = new List<InventoryItemModule>();
     public Transform storageItemsList;
     public Transform equippedItemsList;
 
 public void DisplayFullInventory()
     {
         if (!inventoryPanel.activeInHierarchy)
         {
             inventoryPanel.SetActive(true);
 
             foreach (InventoryItemModule inventoryItem in masterInventoryItems)
             {
                 if (inventoryItem.isStorage)
                 {
                     storageInventoryItems.Add(inventoryItem);
                 }
 
                 if (inventoryItem.isEquippped)
                 {
                     equippedInventoryItems.Add(inventoryItem);
                 }
             }
 
             foreach (InventoryItemModule item in storageInventoryItems)
             {
                 GameObject newButton = Instantiate(button);
                 newButton.transform.SetParent(storageItemsList, false);
                 newButton.GetComponentInChildren<Text>().text = item.ItemName;
                 newButton.GetComponent<Button>().onClick.AddListener(EquipItem);
             }
 
             foreach (InventoryItemModule item in equippedInventoryItems)
             {
                 GameObject newButton = Instantiate(button);
                 newButton.transform.SetParent(equippedItemsList, false);
                 newButton.GetComponentInChildren<Text>().text = item.ItemName;
                 newButton.GetComponent<Button>().onClick.AddListener(StoreItem);
             }
         }
         else if (inventoryPanel.activeInHierarchy)
         {
         storageInventoryItems.Clear();
             equippedInventoryItems.Clear();
 
             inventoryPanel.SetActive(false);
         }
     }
 
     void StoreItem()
     {
     // UPDATE BOOLEAN (isStored or isEquipped)
    // MOVE TO EquippedInventoryItems LIST
         Debug.Log("Store Item");
     }
 
     void EquipItem()
     {
     // UPDATE BOOLEAN (isStored or isEquipped)
    // MOVE TO StorageInventoryItems LIST
         Debug.Log("Equip Item");
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Prevent spawning if the gameobject has same position as a list of Vector3s using another list? 1 Answer

Add Clones of GameObject to List(array) 0 Answers

Instantiating gameObjects, adding them to a list then moving every gameObject in the list.,Moving gameobjects in gameobject list 0 Answers

Having trouble with instantiating bullets at multiple points via a list of transforms. 1 Answer

Check if a large number of gameobjects are colliding 1 Answer


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