• 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
Question by Dontrabajo · Sep 15, 2017 at 01:56 AM · tutorialinventoryadditem

Add Item to inventory - Shop UI intro and setup

Hi there,

as mentioned in the question, I did the Shop UI live training and added a player inventory and a vendor inventory to my game. Now I messing around picking up an item and put it in the players inventory. Probably this question had been asked but I can't find it. Sorry for that. Hope this is the right place to ask beginner questions.

the ShopScrollList (from tutorial link text) script populates the players inventory and sends items to the vendors shop. This works.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class Item
 {
     public string itemName;
     public Sprite icon;
     public float price = 1;
 }
 
 public class ShopScrollList : MonoBehaviour {
     
     public List<Item> itemList;
     public Transform contentPanel;
     public ShopScrollList otherShop;
     public Text myGoldDisplay;
     public SimpleObjectPool buttonObjectPool;
 
     public float gold = 20f;
 
 
     // Use this for initialization
     void Start () 
     {
         RefreshDisplay ();
     }
 
     public void RefreshDisplay()
     {
         myGoldDisplay.text = "Gold: " + gold.ToString ();
         RemoveButtons ();
         AddButtons ();
     }
 
     private void RemoveButtons()
     {
         while (contentPanel.childCount > 0) 
         {
             GameObject toRemove = transform.GetChild(0).gameObject;
             buttonObjectPool.ReturnObject(toRemove);
         }
     }
 
     private void AddButtons()
     {
         for (int i = 0; i < itemList.Count; i++) 
         {
             Item item = itemList[i];
             GameObject newButton = buttonObjectPool.GetObject();
             //newButton.transform.SetParent(contentPanel,false);
             newButton.transform.SetParent(contentPanel,false);
             newButton.transform.localScale = new Vector3 (1.0f, 1.0f, 1.0f);
 
             SampleButton sampleButton = newButton.GetComponent<SampleButton>();
             sampleButton.Setup(item, this);
         }
     }
 
     public void TryTransferItemToOtherShop(Item item)
     {
         if (otherShop.gold >= item.price && otherShop.isActiveAndEnabled) 
         {
             gold += item.price;
             otherShop.gold -= item.price;
 
             AddItem(item, otherShop);
             RemoveItem(item, this);
 
             RefreshDisplay();
             otherShop.RefreshDisplay();
             Debug.Log ("enough gold");
 
         }
         Debug.Log ("attempted");
     }
 
     void AddItem(Item itemToAdd, ShopScrollList shopList)
     {
         shopList.itemList.Add (itemToAdd);
     }
 
     private void RemoveItem(Item itemToRemove, ShopScrollList shopList)
     {
         for (int i = shopList.itemList.Count - 1; i >= 0; i--) 
         {
             if (shopList.itemList[i] == itemToRemove)
             {
                 shopList.itemList.RemoveAt(i);
             }
         }
     }
 }


My attempt: My script attached to a GameObject should get an item to the inventory. Therefore I create a new Item class where I choose 1 or more objects and want to call the AddItem function from the code above to do so:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class pickupitem
 {
     public string itemName;
     public Sprite icon;
     public float price = 1;
 }
 
 public class AddItem : MonoBehaviour {
     public ShopScrollList playerpocket;
     public List<pickupitem> itemListforpickup;
     //public GameObject playerpocket;
     public ShopScrollList shopscrolllist;
 
 
     void OnMouseUp()
     {
         shopscrolllist.AddItem (pickupitem, playerpocket);
     }
 }

Is this a way to approach or is it totally ineffective? Is there a better way? What is wrong with the code? Is there a tutorial that comes after the above mentioned that handles such things?

Thank you guys

Comment

People who like this

0 Show 0
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

118 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

Related Questions

inventory and item system where every item need its own use function 0 Answers

How to select the item inside of my inventory slot? (Followed a tutorial into a rut) 0 Answers

Picking up various types of items? 0 Answers

List check argument of item 3 Answers

int variable from dictionary? 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