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 Bobbagens · Jun 28, 2013 at 03:21 PM · guiinventory

how do i display this as GUI

Sorry for the inpasific "this" but anyway I have an inventory script that I believe works but Im not sure how to display as GUI for the actual inventory part that people can see in game. Heres the code I have- using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class Inventory : MonoBehaviour { // This struct holds the information about an object in the inventory:

public struct InventoryItem

 {


     // This is a reference to the item info provided by your class


     // It should contain information on the item type, and also a picture for use in the inventory.


     public Item Item_Info;


 


     // Quantity of item held:


    public int Number_Held;


 


     // Location of the item in the inventory grid:


     public int X_Location;


     public int Y_Location;





     public InventoryItem(Item item,int number, int x_Loc, int y_Loc)


     {


         Item_Info = item;


         Number_Held = number;


         X_Location = x_Loc;

         Y_Location = y_Loc;


     }


 


     // Increase the number of objects held (or decrease, by passing a negative number):


     public void IncreaseNumber(int number)


     { Number_Held += number; }


 }


 


 public Dictionary<string, InventoryItem> m_Inventory = new Dictionary<string, InventoryItem>();


 


 int next_Free_X = 0;


 int next_Free_Y = 0;


 int width = 10;


 

 void OnTriggerEnter (Collider pCollider) 

{

     // Check that the object can be picked up here


     if(true)  


    {


        // If we already have this object, increase the number we have


         if(m_Inventory.ContainsKey(pCollider.name))


        {


             m_Inventory[pCollider.name].IncreaseNumber(1);


         }


        else


         {


             // If we don't have this object, add a new entry to the inventory

             m_Inventory.Add(pCollider.name, new InventoryItem(GetItemInfo(pCollider), 1, next_Free_X, next_Free_Y));


         


             // Find the next free inventory slot for new items:


             next_Free_X++;


             if(next_Free_X >= width)


             {


                 next_Free_X = 0;


                 next_Free_Y++;


             }
         }


        // Destroy the object picked up:


        Destroy(pCollider.gameObject);

     }


 }

private Item GetItemInfo(Collider pCollider)

 {

// You will need to implement this, you could either have a central list (contained in a gameObject)

// holding references to all possible items, or you could retrieve the information from the object you hit

// with:

return pCollider.GetComponent();

 }


}

how do I display this into GUI.

Comment
Add comment · Show 3
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 Immanuel-Scholz · Jun 28, 2013 at 03:48 PM 0
Share

Could you edit your post to fix the posted script, please? It got totally messed up. A paint to read.

avatar image Deeweext · Jun 28, 2013 at 05:05 PM 0
Share

Even better; If you're not familiar with how to use an OnGUI function, then don't paste in a 150 line script from someone else. Doing so will drown you. Start by tutorials to understand what you're doing. The harsh truth is no one ever success without understanding what they are cutting together.

avatar image gardian06 · Jul 02, 2013 at 09:44 PM 0
Share

please edit provided code to increase readability either precede each line with 4 spaces, or in the toolbar that appears above the box click the box with "101010" between " and attachment.

(note this paragraph was edited down from 2000 chars): aside from placing an if(true) immediately inside any method especially inside of a Unity event without a break anywhere inside is lacking an understanding of what an if directive is even used for, or what an the directive even does if(true == true) will always result in true.

technically your question has no definite answer. if you could show us an "honest" attempt that did not work, or something to that extent we can give you an answer, but otherwise you are asking either for opinions, or "do this for me".

it would appear that the creator of this (guessing tutorial) expects each person to implement their own Item, and OnGUI. considering that this implementation looks like it belongs as a list.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vcjr12 · Jun 28, 2013 at 04:40 PM

To be able to display Items on the GUI, you would have to put a OnGUI() method somewhere on your script.

  void OnGUI() 
     {
         if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
             print("You clicked the button!");
         
     }

More Info on how to use the GUI can be found here and here ,

Comment
Add comment · Share
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

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

18 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

Related Questions

Inventory, why didn't work element? 1 Answer

Help with GUILayout and tooltip(C#) 1 Answer

Click on a button that is created post start. 0 Answers

How to make a simple inventory?!? Beginner coder JS 1 Answer

How to go around creating inventory with new GUI (4.6 Beta)? 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