Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by mrgohut · May 02, 2014 at 06:57 PM · inventory

Inventory - problem with creating new item

Hello :) I have Inventory, but i need help.

Here is my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
 
     public SimpleMouseRotator camera1;
     public SimpleMouseRotator camera2;
 
     public int slotsX, slotsY;
 
     public GUISkin skin;
 
     public List<Item> inventory = new List<Item>();
     public List<Item> slots = new List<Item>();
 
     public bool showInventory;
     private bool showTooltip;
     private bool draggingItem;
     private Item draggedItem;
 
     private string tooltip;
 
     private ItemDatabase database;
 
     private int prevIndex;
 
     void Start()
     {
         camera1 = GameObject.FindWithTag("Player").GetComponent<SimpleMouseRotator>();
         camera2 = GameObject.FindWithTag("MainCamera").GetComponent<SimpleMouseRotator>();
         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
 
         for(int i = 0; i < (slotsX * slotsY); i++)
         {
             slots.Add(new Item());
             inventory.Add(new Item());
         }
         AddItem(0);
         AddItem(1);
         AddItem(2);
     }
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Y))
         {
             AddItem(2);
         }
         if(Input.GetKey(KeyCode.U))
         {
             AddItem(2);
         }
         if(Input.GetKeyDown(KeyCode.Tab))
         {
             showInventory = !showInventory;
         }
         if(showInventory)
         {
             camera1.enabled = false;
             camera2.enabled = false;
         }
         else
         {
             camera1.enabled = true;
             camera2.enabled = true;
         }
     }
 
     void OnGUI()
     {
         tooltip = "";
         GUI.skin = skin;
         if(showInventory)
         {
             DrawInventory();
             if(showTooltip)
                 GUI.Box(new Rect(Event.current.mousePosition.x + 15f, Event.current.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
         }
         if(draggingItem)
         {
             GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
         }
     }
 
     void DrawInventory()
     {
         Event e = Event.current;
         int i = 0;
         for(int y = 0; y < slotsY; y++)
         {
             for(int x = 0; x < slotsX; x++)
             {
                 Rect slotRect = new Rect(x * 60, y * 60, 50, 50);
                 GUI.Box(slotRect, "", skin.GetStyle("Slots"));
                 slots[i] = inventory[i];
                 int itemCount = inventory[i].itemCount;
                 if(slots[i].itemName != null)
                 {
                     GUI.DrawTexture(slotRect, slots[i].itemIcon);
                     GUI.Label(new Rect(x * 60,y * 60,50,50), itemCount.ToString());
                     if(slotRect.Contains(e.mousePosition))
                     {
                         tooltip = CreateTooltip(slots[i]);
                         showTooltip = true;
                         //przenoszenie przedmiotów - podnieś
                         if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
                         {
                             draggingItem = true;
                             prevIndex = i;
                             draggedItem = slots[i];
                             inventory[i] = new Item();
                         }
                         //przenoszenie przedmiotów - upuść
                         if(e.type == EventType.mouseUp && draggingItem)
                         {
                             inventory[prevIndex] = inventory[i];
                             inventory[i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
                         //Używanie przedmiotów
                         if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
                         {
                             print("Clicked " + i);
                             if(inventory[i].itemType == Item.ItemType.Consumable)
                             {
                                 if(inventory[i].itemCount == 1)
                                 {
                                     UseConsumable(slots[i], i, true, false);
                                 }
                                 else
                                 {
                                     UseConsumable(slots[i], i, false, true);
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     if(slotRect.Contains(e.mousePosition))
                     {
                         if(e.type == EventType.mouseUp && draggingItem)
                         {
                             inventory[i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
                     }
                 }
                 if(tooltip == "")
                 {
                     showTooltip = false;
                 }
                 i++;
             }
         }
     }
 
     //wyświetlanie opisu przedmiotu po najechaniu na niego myszką
     string CreateTooltip(Item item)
     {
         tooltip = "<color=#ffffff>" + item.itemName + "</color>\n\n" + item.itemDesc;
         return tooltip;
     }
 
     void RemoveItem(int id)
     {
         for(int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemID == id)
             {
                 inventory[i] = new Item();
                 break;
             }
         }
     }
 
     void AddItem(int id)
     {
         for(int i = 0; i < inventory.Count; i++)
         {
             //Jeżeli nie istnieje, dodaj item
             if(inventory[i].itemName == null)
             {
                 for(int j = 0; j < database.items.Count; j++)
                 {
                     if(database.items[j].itemID == id)
                     {
                         inventory[i] = database.items[j];
                     }
                 }
                 break;
             }
             //Jeżeli istnieje dodaj +1 do licznika
             else
             {
                 if(inventory[i].itemID == id)
                 {
                     if(inventory[i].itemCount != 200)
                     {
                         inventory[i].itemCount += 1;
                         break;
                     }
                 }
             }
         }
     }
     /*
     void AddItemCount(int id)
     {
         for(int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemName != null)
             {
                 inventory[i].itemCount += 1;
                 break;
             }
         }
     }
     */
     bool InventoryContains(int id){
         foreach(Item item in inventory){
             if(item.itemID == id) return true;
         }
         return false;
     }
 
     //Używanie itemów
     private void UseConsumable(Item item, int slot, bool deleteItem, bool deleteCount)
     {
         //switch (zostawić)
         switch(item.itemID)
         {
             //case id, np. jabłko ma id 4 -> case 4: .... coś zrób
         case 2:
         {
             print("Mnom, mnom, mnom");
             break;
         }
         }
         //jeżeli wysłana informacja o usunięciu
         if(deleteItem)
             inventory[slot] = new Item();
         //jeżeli wysłana informacja o zmianie licznika
         if(deleteCount)
         {
             for(int i = 0; i < inventory.Count; i++)
             {
                 if(inventory[i].itemID == item.itemID)
                 {
                     inventory[i].itemCount -= 1;
                     break;
                 }
             }
         }
     }
 }
 

I have stackable item. Max stacks - 200. And now, i start from 1, when i hold "Y" key it add stacks to max, 200, but here is my problem when it reach max 200 stacks it create new item (that what i want) with 1 stack but when i still adding stacks it change both items to 1 and adding stacks to max, 200. You got it ?

I really need help with this ;/

Comment
Add comment · Show 2
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
avatar image Oribow · May 02, 2014 at 07:24 PM 0
Share

Have you ever heard from: else if ()? You dont have to use else and the if

avatar image mrgohut · May 02, 2014 at 07:47 PM 0
Share

but else if is a little crap and .. i dont need it here i think, in what it can help me , coz i really dont see it in my code now ;p

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

21 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

Related Questions

Creating an interactable inventory? 1 Answer

Inventory stack system problam 0 Answers

GUI focus control - inventory system issue 1 Answer

UI: Dynamically sized inventory window with scroll 0 Answers

enum comparing 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