• 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 William_Weaver · May 01, 2014 at 02:38 AM · arraylistinventorymultipledata

Store multiple types of data in an Array?

Evening everyone! Here's the deal: I'm setting up an inventory system the stores multiple kinds of information on the items that occupy each slot. Each inventory slot needs to house the object's 1) Name, 2) Tag, 3) Prefab Object, 4) Amount, 5) MaxStack.

The way I've set this up for versatility, is that every object that can be 'collected' is a Prefab that includes a script that houses the above information in separate variables, and when the player interacts with the object, I need it to package that info into an Array or List, SendMessage("Inventory", Array/List) to the player's inventory script, and be unpacked by a function in that script to compare the item's info against what's already stored to find the next available space.

SO! In theory, this should work using Javascript Array or an ArrayList, but on unpacking, I repeatedly get Implicit Downcast warnings from type 'Object' to type 'String, int, etc.', even with type casting like: var itemName : String = array[0];

I'm working in UnityScript(variant of Javascript), but translating to/from C# isn't hard, so any help would be appreciated. ^_^

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

4 Replies

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

Answer by Clet_ · May 01, 2014 at 03:45 AM

Let's say your class looks like this :

 public class MyClass {
     public int myInt;
     public string myString;
     public bool myBool;
 }

In your other scripts, you can have :

 List<MyClass> list = new List<MyClass>();

When "unpacking", it should look like this :

 MyClass unpackedItem = list[0];
 Debug.Log(unpackedItem.myString);
Comment
Add comment · Show 4 · 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 William_Weaver · May 01, 2014 at 04:19 AM 0
Share

Found a series of Unity Tutorials on Advance Inventory systems, and they're helping so far. Seems the issue I had was I didn't create the class correctly, as in I didn't make the new class it's own script. I though it could just be declared in one. ^_^;

I'll test this out (with the class made appropriately this time) and if it works out, this'll be the answer I needed.

avatar image William_Weaver · May 01, 2014 at 05:03 AM 0
Share

The new class is definitely the solution I needed. Just need to be certain to mention that creating a new class in JS requires you to make a new script that 'is' that class, named the same and everything, and it's variables are defined in that script.

avatar image Clet_ · May 01, 2014 at 09:34 AM 0
Share

I don't know in JS, but in C#, you can declare non-$$anonymous$$onoBehaviour script anywhere you want.

In this particular case of an inventory, you definitely don't need to inherit from $$anonymous$$onoBehaviour

avatar image honor0102 · Jun 03, 2019 at 08:35 AM 0
Share

to those who need to use this at inspector:

use [System.Serializable] for $$anonymous$$yClass or some way similar to this way:

https://answers.unity.com/questions/324167/builtin-array-of-different-data-types-to-inspector.html

according to Loius answer and robertbuof comment:

  [System.Serializable]
  public class $$anonymous$$ultitype {
    public int x;
    public string y;
    public GameObject z; 
  }
      public $$anonymous$$ultitype[] things;
avatar image
0

Answer by 0V3RR1D3 · May 01, 2014 at 03:14 AM

Hey, check this out, Hope it helps!

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 William_Weaver · May 01, 2014 at 03:30 AM 0
Share

I've tried a lot of that already with the same general problem. I can put the values in, but getting them out becomes the problem. A Javascript Array or an ArrayList gives me implicit downcast warnings, and a Custom Class array (may have been a bit above my experience level) threw a whole bunch of errors about not supporting splitting when I tried to retrieve information from some of the fields. :\

At the moment I'm using the inventory function as a public one and calling it directly, but I would REALLY like to be able to store and retrieve this data for more compact scripting. Right now every inventory slot has 5 separate variables a piece. @_@;

avatar image
0

Answer by HydraOrcUnity · Jun 12, 2021 at 04:20 PM

 var data = new object[] { 1, "string", 1.2f, 2 };

This data format is useful for for sending JSON messages, for example using UltimateJson:

 Debug.Log(JsonObject.Serialise(data));

Which will result in such array (this approarch generates an object instead of array when serialising classes):

 [1,"string",1.2,2]

Also you can store this data in a database like MongoDB

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 PhyrePhrost · Jun 12, 2021 at 05:33 PM

you can use simply the type 'object' as an array and pass into it the variables... when you need one of them you can check is original type with GetType()

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

25 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 avatar image avatar image avatar image avatar image

Related Questions

List array in GUI.Box 0 Answers

Limiting size of data structures in editor 1 Answer

Scroll List Problem 1 Answer

How can I access the variables of arrays with multiple data types? 1 Answer

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