- Home /
Question by
kaluxx · Aug 11, 2017 at 04:53 PM ·
listsinventory system
unity2d inventory old style (like final fantasy) storing items amount issue C#
-2 down vote favorite I was working in an simple 2d rpg game and I was working on a simple inventory system with only UI TEXT (not icons).
For example, when I open a chest and find 1 potion and then open another chest and find another potion I need to destroy that potion and increase the quantity of the first potion by 1, but it doesn't work correctly.
When I use random.range and put multiple items to randomise, it doesn't work, but when I use only one item it works (ex random.range (0.0) )!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class inventoryTest : MonoBehaviour
{
public Transform panel;
public List<Item> Inventory = new List<Item>();
public ItemDatabase data;
public GameObject Menu;
public bool pressed;
public Text testText;
private void Start()
{
data = GameObject.FindGameObjectWithTag("database").GetComponent<ItemDatabase>();
}
private void Update()
{
for (int i = 0; i < Inventory.Count; i++)
{
testText.name = Inventory[i].Itemname;
testText.text = Inventory[i].Itemname + " : " + Inventory[i].capacity;
}
if (Input.GetKeyDown(KeyCode.I) && !pressed)
{
pressed = true;
Menu.SetActive(true);
Time.timeScale = 0;
}
else if (Input.GetKeyDown(KeyCode.I) && pressed)
{
Time.timeScale = 1;
pressed = !pressed;
Menu.SetActive(false);
}
}
public void CheckForItemExist(int ID, Item item)
{
for (int i = 0; i < Inventory.Count; i++)
{
if (Inventory[i].ItemId == ID)
{
item = data.items[i];
//Inventory[i] = new Item(item.ItemId,item.Itemname,item.ItemPower,item.Itemtype,item.capacity) ;
Inventory[i].capacity = item.capacity;
item.capacity += 1;
Inventory[i].Itemtype = item.Itemtype;
Inventory[i].ItemPower = item.ItemPower;
Inventory[i].Itemname = item.Itemname;
Inventory[i].ItemId = item.ItemId;
foreach (Item items in Inventory)
{
if (Inventory[i] == item)
{
Inventory.Remove(item);
Destroy(testText.gameObject);
}
}
break;
}
}
}
private void AddEmpty(Item item)
{
for (int i = 0; i < Inventory.Count; i++)
{
if (Inventory[i].Itemname == null)
{
Inventory[i] = item;
//
break;
}
}
}
public void DesplayInventory()
{
for (int i = 0; i < Inventory.Count; i++)
{
Text _text = Instantiate(Resources.Load("Text", typeof(Text)), new Vector2(panel.transform.position.x, panel.transform.position.y * i), Quaternion.identity) as Text;
testText = _text;
_text.transform.SetParent(panel);
_text.transform.localScale = new Vector3(1, 1, 1);
Debug.Log(testText);
break;
}
}
}
and here is the chest script
using System.Collections;
using System.Collections.Generic;
public class generateItem : MonoBehaviour
{
public Sprite open;
private inventoryTest inv;
private test test;
public bool opened;
private ItemDatabase data;
private void Start()
{
test = GameObject.FindGameObjectWithTag("Player").GetComponent<test>();
inv = GameObject.FindGameObjectWithTag("Player").GetComponent<inventoryTest>();
data = GameObject.FindGameObjectWithTag("database").GetComponent<ItemDatabase>();
}
private void Update()
{
if (opened)
{
Destroy(this);
}
}
private void OnCollisionStay2D(Collision2D other)
{
if (other.collider.tag == "Player" && Input.GetKeyDown(KeyCode.Return))
{
if (!opened)
{
var value = Random.Range(1, 2);
inv.CheckForItemExist(value, inv.data.items[value]);
inv.Inventory.Add(inv.data.items[value]);
inv.DesplayInventory();
7gpjs.png
(73.9 kB)
sans-titre-1.jpg
(59.8 kB)
Comment