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
-1
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();

alt text

7gpjs.png (73.9 kB)
sans-titre-1.jpg (59.8 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

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

67 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

Related Questions

Issue with equipping object 0 Answers

How to get the current items in my inventory with respective stock of the items 1 Answer

Add to element quantity value if it already exists 0 Answers

Select all elements in a list? 1 Answer

Displaying a List or an Array in Editor file 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