• 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
Question by Nokirar · Jan 03, 2013 at 10:20 AM · inventory systemlibraryitems

Best way of doing Item class?

Hello! I want to make a good base-class for items so i can easily add new items and itemTypes. I was wondering if there is a way to access different types of data from an item ID, i'm sorry if i'm bad at explaining, maybe if you look at my code you can understand what i mean and maybe point me in the right direction!

What i want to be able to do is write something like this to access the data:

readItemData(001).name;

Is what i'm trying to do possible? Am i doing item classes as good as possible or is there better ways to do it?

 using UnityEngine;
 using System.Collections;
 
 public class itemInfo : MonoBehaviour {
     
     
     public int universalStackLimit;
     
     public int[] IDs;
     public string[] Names;
     public GameObject[] Models;
     public string[] Types;
     public GUIContent[] IconImages;
     public bool[] stackables;
 
     
     public void assignItemData(int ID, string Name, GameObject Model, string itemType, GUIContent IconImage, bool stackable)
     {
         IDs[ID] = ID;
         Names[ID] = Name;
         Models[ID] = Model;
         Types[ID] = itemType;
         IconImages[ID] = IconImage;
         stackables[ID] = stackable;
     }
     
     public void readItemData(int ID)
     {
         
     }
 }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Dave-Carlile · Jan 03, 2013 at 01:56 PM

You probably want to use a class or a struct for this instead.

e.g.

 struct Item
 {
   public int ID,
   public string Name,
   public Model GameObject,
   public string Type,
   public GUIContent IconImage,
   public bool Stackable
 }


Store the structs in a list using generics...

 List<Item> items = new List<Item>();


Add one to the list...

 Item item = new Item {
   ID = 10,
   Name = "AnItem",
   Model = someModelReference,
   Type = "Test",
   IconImage = someIconImageReference,
   Stackable = true
 }

 items.Add(item);


And so on. Look up generic arrays and structs for more info. You could also use a class rather than a struct and things would look mostly the same. Depends on your needs.

Comment
megabrobro

People who like this

1 Show 3 · 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
avatar image Nokirar · Jan 03, 2013 at 04:19 PM 0
Share

Thank you! I tried something similar with enum, but struct seems much better, this looks much cleaner than my way of doing it!

avatar image Nokirar · Jan 04, 2013 at 08:51 AM 0
Share

List, is not available, IList is though. It seems to work the same way, the only thing i cant get to work is items.Add(item);

avatar image Nokirar · Jan 04, 2013 at 08:59 AM 0
Share

Never mind, added using "System.Collections.Generic;" and it worked.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Equipting a Item/Weapon 1 Answer

Inventory and scriptable object items - please point me in the right direction 1 Answer

Parse different types of objects from Json 1 Answer

Creating a custom List or Collection 1 Answer

Access to slot Item 0 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