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][1]) 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- 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.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 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
[1]: https://www.youtube.com/watch?v=TAJCr3_kfEc
↧