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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by dylfahr · May 04, 2015 at 11:28 PM · c#guigameobjectarraylist

How to select an Game Object from an Item List

I have an inventory system that uses a list to create icons and all, but I want the icons to represent the game objects listed in an array. I want to be able to click on the icon and drag out a game object representing that item in the inventory list. C Sharp only

here is the code for the items: using UnityEngine; using System.Collections;

 [System.Serializable]
 public class item {
     public string itemName;
     public int itemID;
     public string itemDisc;
     public Texture2D icon;
 
 
     public item(string name, int id, string desc){
         itemName = name;
         itemID = id;
         itemDisc = desc;
         icon = Resources.Load<Texture2D> ("Icons/" + name);
 
     }
     public item(){
 
         }
 
 }


and here is the code for the inventory that I am working with:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     public int slotsX, slotsY;
     public List<item> inventory = new List<item>();
     public List<item> slots = new List<item>();
     private bool showInventory;
     private itemDatabase database;
     private bool showToolTip;
     private string toolTip;
     private bool dragItem;
     private item draggedItem;
     public GameObject[] items;
     private itemPlacement ItemPlacement;
 
     // Use this for initialization
     void Start () {
         ItemPlacement = GetComponent<itemPlacement> ();
         for(int i = 0; i < (slotsX * slotsY); i++){
             slots.Add(new item());
             inventory.Add (new item());
         }
         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<itemDatabase> ();
         addItem (0);
         addItem (1);
         addItem (2);
         addItem (3);
 
     }
     void Update(){
         if(Input.GetButtonDown("Inventory Menu"))
            {
             showInventory = !showInventory;
         }
     }
     
     // Update is called once per frame
     void OnGUI () {
         toolTip = "";
         if(showInventory)
         {
             drawInventory ();
         }
         if (showToolTip){
             GUI.Box (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 50),toolTip);
 
             }
         if (dragItem){
             GUI.DrawTexture (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50),draggedItem.icon);
         }
 
     }
     void drawInventory(){
         Event e = Event.current;
         int i = 0;
         for (int x = 0; x < slotsX; x++) {
             for (int y = 0; y < slotsY; y++) {
                 Rect slotRect = new Rect (x * 60, y * 60, 60, 60);
                 slots [i] = inventory [i];
                 if (slots [i].itemName != null) {
                     GUI.DrawTexture (slotRect, slots [i].icon);
                     if (slotRect.Contains (Event.current.mousePosition)) {
                         toolTip = createToolTip (slots [i]);
                         showToolTip = true;
                         if (e.button == 0 && e.type == EventType.MouseDrag && !dragItem) {
                             dragItem = true;
                             draggedItem = slots [i];
                         }
                     }
                     for (int g = 0; g <items.Length; g++) {
                         if (e.type == EventType.mouseUp && dragItem) {
                             dragItem = false;
                             draggedItem = null;
                             g = slots[i].itemID;
                             ItemPlacement.SetItem (items [g]);
                         }
                     }
 
                     if (toolTip == "") {
                         showToolTip = false;
                     }
                     i++;
                 }
             }
         }
     }
     
     string createToolTip (item Item){
         toolTip = Item.itemName +  "\n" + Item.itemDisc;
         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++) {
             if (inventory [i].itemName == null) {
                 inventory [i] = database.items[id];
                 break;
             }
         }
     }
 
 }
 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make a or array of GUI.Button's? 1 Answer

Can't Activate a GameObject from Array 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

How would I find the name of all game objects in a c# List 1 Answer

How to put gameObjects to the list? 4 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