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 bubzy · Sep 29, 2014 at 09:31 AM · arrayinspectorenum

populate array values automatically in inspector

ill try and make this brief.

I have a project that I am experimenting with, it has a "crafting" and resource collecting system in place. all items in the game are stored in a single class as an enum and then everything that uses that enum calls it at runtime to populate itself.

for example

     [System.Serializable]
     public class testItem
     {
     public enum itemList {coal,gold,silver}; //you get the idea
     }
     
     [System.Serializable]
     public class recipeList
     {
     public string itemName;
     public bool craftable;
     public testItem.itemList ingredientOne;
     public testItem.itemList ingredientTwo;
     //and so on for each ingredient for the item(if it's craftable)
     }
     
     public class craftItems : MonoBehaviour 
     {
     
     public recipeList[] allItems = new recipeList[100]; //or whatever value
     
     void start()
     {
         for(int i = 0; i < allItems.Length; i++)
             {    
     allItems[i].name =Enum.GetName(typeof(testItem.itemList),i);
             }
     
     }//start()
     }//class craftItems
     
     
     *****and in another separate script.......
     
     [System.Serializable]
     public class inventory
     {
     public string name;
     public int quantity;
     
     public void adjustStock(int amount)
     {
     quantity += amount;
     }
     
     }//class inventory
     
     public class mainBase : MonoBehaviour
     {
     public inventory[] inStock = new inventory[100];
     
     
     void Start()
     {
     for (int i = 0; i < inStock.Length; i++)
     {
     inStock[i].name = Enum.GetName(typeof(testItem.itemList),i);
     }
 
     }//start()
     }//class



so, this works fine, its not a bad system and it allows me a great deal of flexibility when it comes to assigning resource points, and workers to collect them, the recipe system works really well also.

after all this code, my question really is,

can i populate the inStock[] array and the recipeList[] array before runtime? this would allow me to create recipes easily and also adjust starting stock levels and so on, sorry if this has been a drawn out example, but im scratching my head a bit here as to what to do.

please ignore syntax errors, this is not my original code, i wrote it for the question.

bubzy.

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

2 Replies

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

Answer by bubzy · Oct 02, 2014 at 11:01 PM

I actually found out that I can

 [ExecuteInEditMode]

before the classes, this way everything is populated prior to running the game,

the caveat is that you cannot affect certain objects properties

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 dmg0600 · Sep 29, 2014 at 09:50 AM

If you want it to be length fixed you can always assign new values to the elements of an array almost as you are assigning their names in your script.

 inStock[index] = whatever;

If you want it to be more flexible and to be able to add and remove elements, just use lists insted of arrays:

 public List<inventory> inStock = new List<inventory>();

To add and remove objects just use the Add and Remove methods from the list. you can take a look to lists use 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

26 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

Related Questions

Enum drop down menu in inspector for nested arrays 2 Answers

Creating enum using a string array 3 Answers

Making a user-friendly array with indices based on an enum 2 Answers

Array with multiple variables in Inspector 1 Answer

Rename array element name in inspector with enum 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