• 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 DynamicPrgm · Jan 21, 2014 at 01:44 AM · arraylistobjectsinventorystorage

Referencing a struct/class From Another Script

I have been developing an inventory system and am curious now on how I should store the actual items. Not looking for a solution for a lazy person or anything, I like to learn.

The way my inventory works so far, is basically a 2D array which has a coordinate that corresponds to the value that is stored in the array parameters.

alt text

alt text

At the moment the boxes only store ints. I want them to store structs or classes that hold the data for objects. I want to know if I create another script that contains the list of struct/classes that has the objects, could I somehow use that struct/class as a type for the array holding the objects? Simple example:

 //Item script
 public class Items : MonoBehaviour {
      struct Item {
           string name;
           int value;
           GameObject item_mesh;
      }
 
      //List here, etc.     
 }
 
 
 //Inventory script
 public class Inventory : MonoBehaviour {
      Item[,] storage; //Item from the Items script
      //rest of inventory stuff here
 }
a.png (20.6 kB)
b.png (13.8 kB)
Comment
Add comment · Show 1
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 DynamicPrgm · Jan 21, 2014 at 01:46 AM 0
Share

How could I reference that type from that script?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MrCrinkle · Jan 21, 2014 at 01:53 AM

It shouldn't be a problem as long as both classes are in the same namespace. If not, you'll have to include the class you want to use in the other script. In your case you would have

 using [Namespace].Item;

in your inventory script.

Comment
Add comment · Show 1 · 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 DynamicPrgm · Jan 21, 2014 at 01:59 AM 0
Share

Okay, thank you! I will see if it works. If not, I will see if what Broxxar suggested solves the issue.

avatar image
0

Answer by MakinStuffLookGood · Jan 21, 2014 at 01:54 AM

 struct Item { .. }

That struct is private within Items scope. If the struct were public, you could use it from another class as

 Items.Item myItem = new Items.Item();

It might even be nicer to just have the struct outside the scope of the Items class so you can use it as

 Item myItem = new Item();
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
avatar image
0

Answer by HappyMoo · Jan 21, 2014 at 02:01 AM

How about something like that:

 public enum ItemType {    None, Weapon, Tool, Food, Resource, Construction  }
 
 [System.Serializable]
 public class Item {
     public string Name;
     public int ID;
     public string Description;
     public int MaxStack;
     public ItemType Type;
  
     public GameObject ItemPrefab;
     public Texture2D Icon;
 }
 
 [System.Serializable]
 public class ItemStack
 {
     public int Size;
     public Item Item;
 }


I wrote something up in the forums for a similar question:

http://forum.unity3d.com/threads/220674-Designing-Inventory-managment-and-Inspector-driven-Item-definition-for-Crafting-Game

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.

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

20 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

Related Questions

List array in GUI.Box 0 Answers

A node in a childnode? 1 Answer

Scroll List Problem 1 Answer

Which is better for an inventory system, Array or List? 0 Answers

Preventing Items from shifting in a list 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