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
4
Question by jjobby · Sep 09, 2010 at 08:50 PM · arrayserialization

Can I define initial variable for serialized array in the inspector?

Here is my example code.

[System.Serializable] public class Test { public int p; public float v; public Color c;

 public Test()
 {
     p = 2;
     v = 4.4f;
     c = Color.yellow;
 }

}

public class Example : MonoBehaviour { public Test test;

 public Test[] testArray;

}

The result is that test and testArray variables are editable in the inspector. test is properly started with initial value in the constructor of Test class. But for testArray, when I edit array size in the inspector, the initial value of those variables are always system default. (p = 0, v = 0, c = Color.black) It isn't used the initial value in the constructor. I've already tried to define value in the declaration part but it doesn't help. Is there a way to work around this problem? This is just a test code. I intend to have many more editable variables in the class. Without initial value, it would be a pain to edit all of those variables again and again when increasing the array in the inspector.

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

6 Replies

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

Answer by Mike 3 · Sep 24, 2010 at 03:10 PM

Possible using the constructor:

public class Example : MonoBehaviour { public Test test;

 public Test[] testArray;

 public Example()
 {
     Test a = new Test();
     a.p = 5;
     Test b = new Test();
     b.p = 100;
     testArray = new Test[] { a, b };  //your array will start with a and b in the inspector by default
 }

}

Note: using the constructor for monobehaviours is usually a bad idea, but this is one place that it'll work well, as it'll create the array before the deserializer adds in any values. It will however mean that the array is assigned twice at runtime - You can however comment/remove the code before making a final build though

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 slippdouglas · Jan 16, 2015 at 04:19 AM 0
Share

-1: This won't assign the default values when the array is lengthened via the Editor's Inspector.

avatar image
-1

Answer by qJake · Sep 09, 2010 at 08:57 PM

No. Array values must be initialized manually. If you have Test[], the type is actually "Array of Tests", which is not the same as a "Test". Anytime you have an array, each cell has to be initialized independently. Unity handles this for you because of the inspector, so it just assigns the default/null values to the type. If you want to be able to control the values, or assign them via the constructor, you need to do something like this:

public Test[] arr;

void Start() { arr = new Test[10]; foreach(Test t in arr) { t = new Test(); } }

That will initialize all of the values in the array. Unity does not do this for you, as far as I know.

Comment
Add comment · 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 jjobby · Sep 09, 2010 at 09:26 PM 0
Share

Ahh... But I want it to be editable by user before the script is run. The script will use those value to do the work. So, doing the initialize in Start() isn't something I want to do. Well, if I can't initialize value for the array then I think that I will write the script to check if the value is left zero or not. If so then it will be reset to my default value in Start()

Thanks for the answer!

avatar image Jesse Anders · Sep 10, 2010 at 12:17 AM 0
Share

One option might be to create a custom inspector for your class. You could display the default inspector, but also (for example) include a control that would initialize the array elements to the desired values.

avatar image
-1

Answer by standardcombo · Jul 30, 2014 at 09:51 AM

This is how you do it. When Unity creates the 'myStrings' property for viewing in the inspector, it will call the ArrayInitializer() constructor, assigning the default values you pass as parameter:

 public MyClass : MonoBehavior
 {
     [System.Serializable]
     public class ArrayInitializer
     {
         public string[] values;
         public ArrayInitializer(string[] defaults) { values = defaults; }
     }
 
     public ArrayInitializer myStrings = new ArrayInitializer(new string[]{"a", "b", "c"});
 }
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 standardcombo · Jul 30, 2014 at 10:15 AM 0
Share

You can go as far as overloading the [] and adding a Length getter so its transparent from the point of view of the code using the property, so you don't have to go through .values each time.

avatar image
0

Answer by Sprakle · Feb 07, 2015 at 04:28 AM

Old question, but I had the problem myself and came across this while searching. @mike-3 has an okay solution, but it doesn't work when new items are added to the list.

Eventually I figured out something that works pretty well. It uses the wonderful OnValidate() message.

     [Serializable]
     class Test
     {
         public float size;
         public string food;
 
         public Test()
         {
             // This ensures the defaults are set in normal non-array situations.
             SetDefaults();
         }
 
         public void SetDefaults()
         {
             size = 10;
             food = "Pizza";
         }
     }
 
     class TestBehaviour : MonoBehaviour
     {
         public Test[] tests;
 
         // OnValidate gets called by the editor whenever a value is changed. Awesome!
         private void OnValidate()
         {
             foreach (Test test in tests)
                 test.SetDefaults();
         }
     }
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 Edy · Mar 06, 2015 at 01:41 PM 2
Share

I can't see the point of this. It means that whenever a value is changed in the inspector, the OnValidate method resets the array back to the defaults.

avatar image
0

Answer by asteroidpilot · Apr 28, 2021 at 12:34 PM

Still no answered?

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
  • 1
  • 2
  • ›

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accesing custom class object from another script 1 Answer

Picking a Random Order for Levers 1 Answer

Adding variables from all scripts 2 Answers

how to build & populate an array available to all scripts on all objects? 1 Answer

Instantiate duplicates script in subsequent Objects 2 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