• 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 MezFo · Jul 15, 2018 at 09:31 PM · monobehaviourdata storageobject reference

What are good ways to store a simple set of data/variables on an object

Hi, I am currently working on a project that involves a slot inventory system and everything about how the items being picked up/used/crafted etc, are identified is from a set of variables in a script on the object representing the item or icon representing it in the inventory. This is working great but the script I am storing them in is just a normal monobehaivour script with public variables that i fill in inspector. I am assuming that because the script is literally just holding some variables and not actually doing/running anything that there may be a better way to store the variables on the item objects. Also like mentioned I need to make these variables public which by my understanding is good to avoid if possible.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Stratosome · Jul 15, 2018 at 09:46 PM

So, you have a MonoBehaviour that is just a set of variables? And do you have another MonoBehavior then that actually manages your inventory system and just gets the other scripts variables? You could always make just a basic class that doesn't extend MonoBehaviour and then make a variable for that class in your main script. A struct could also do the same (similar to a class, but isn't passed around by reference). If you need to be able to modify these variables, there are a few options. If you make a class or struct to hold these variables, you could add [System.Serializable] above the class/struct to get it to show up in the inspector. Another cool way to go about this is to use ScriptableObjects. It's a way to kind of make a prefab that holds configuration settings for something in your scene. Or that's at least one example of how to use them. I also don't think that just having a MonoBehaviour to hold some variables for inventory settings isn't all that bad. Not sure what the BEST way to do it is, but those are some options.

Comment

People who like this

0 Show 2 · 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 MezFo · Jul 15, 2018 at 10:26 PM 0
Share

Just been reading the docs on those, looks like exactly what i'm looking for. Thanks!

avatar image Stratosome MezFo · Jul 15, 2018 at 10:39 PM 0
Share

Awesome. Let me know if you have other questions on them. It's about the best I could do given the details of your initial question.

avatar image

Answer by ShadyProductions · Jul 15, 2018 at 09:38 PM

I usually make an xml file with the item structure in it, where I setup all my items. And write some custom xml parser class to load in all my items at startup based on the xml file using Linq-to-xml, This also allows (mod support) in the future if people wanna add items of their own. Another good way is to use Scriptable Objects.

Comment

People who like this

0 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 Stratosome · Jul 15, 2018 at 09:47 PM 0
Share

Yup, that's another way to do it. Haven't used xml stuff myself yet.

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

88 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 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 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 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

Serialize loads of different data 0 Answers

Best way to store NPC data and how to do it? 1 Answer

Object reference not set to an instance of an object error 2 Answers

NullReferenceException Issues 3 Answers

Multiplayer not using isLocalPlayer 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