Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by n_rusev · Mar 29, 2020 at 07:37 AM · editorclasses

Editor not showing default values

Hello. I have the following classes:

 [System.Serializable]
 class A
 {
     public float a = 50f;
     public float b = 100f;
     //rest of class...
 }
 
 [System.Serializable]
 class B
 {
     public A[] myArray;
     //rest of class...
 }
 
 class C : MonoBehaviour
 {
     public B myB;
     //rest of class...
 }

For some reason every time I create a new element for "myArray" in the editor, it gets created with a=0 and b=0, and I would want to have the default values 50f and 100f. Has anyone encountered this and know what causes it? I should also mention that I have a custom editor for class C that just adds some buttons like so:

 [CanEditMultipleObjects]
 [CustomEditor(typeof(C))]
 public class C: Editor
 {
 
     public override void OnInspectorGUI()
     {
         if(GUILayout.Button("Do something"))
         {
             //....
         }
         base.OnInspectorGUI();
      }
 }

Thank you for your time :)

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

Answer by streeetwalker · Mar 29, 2020 at 07:48 AM

@n_rusev, you have constructors in each of your classes, and you are calling the constructors to create instances of them? And you are creating new instances of A and adding them to your array in class B?

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 n_rusev · Mar 29, 2020 at 08:09 AM 0
Share

No, as you can see class C extends $$anonymous$$onoBehaviour, which means I'm editing the public values from the editor.

avatar image streeetwalker n_rusev · Mar 29, 2020 at 08:41 AM 0
Share

But wait, when you declare, public A[] myArray, myArray does not get populated with anything automatically. You have to dimension the array first and then you have to instance the objects and assign them to the array elements.

Otherwise, how do you imagine it working? How many array elements do want to display of class A's default values? For argument, let's say, 10. Without actual instances of class A, when you alter the 9th element default values, what of A gets those values? Without instances of A, and B, what you are trying to do won't make sense.

avatar image streeetwalker n_rusev · Mar 29, 2020 at 09:03 AM 1
Share

@n_rusev, I have to clarify: it is a bit deceiving what Unity does with an inspector.

When you declare a public array of simple variable type, float, or struct type Vector3, for example, and dimension it using the inspector, it will create an array populated with those types using the values you entered in for each element when you run the game.

However it does not do the same for objects. Declaring an array of GameObject, or any other type of object that needs to be instantiated, does not create an array of those actual objects - only an array of empty place holders of that type.

I think the only way around your problem is to have script C [ExecuteInEdit$$anonymous$$ode] and get the dimension you've entered into the inspector, and then populate the array with actual instances of A. That may be a trick to do, and have it stick when the game runs.

avatar image n_rusev streeetwalker · Mar 29, 2020 at 10:45 AM 0
Share

Thank you for your replies. Your second reply was very helpful. I see now why I get this problem - as you said unity creates only empty place holders for the future objects. I forgot to mention that I only needed the first element to have the default values, since every other copies the previous and that is fine for what I'm trying to do and in my case actually it won't make sense to use this script with empty array in class B, so just changing the code from public A[] myArray; to public A[] myArray = new A[1]; populated the fields with the default values.

avatar image
0

Answer by Lyje · Feb 12 at 06:30 PM

Assuming the containing class is a MonoBehaviour or ScriptableObject, there's a workaround for this to automatically replace editor-created instances with correctly constructed instances.

 [System.Serializable]
 public class MyPlainClass {
     public float a = 100;
     public bool isInitialized = true;
 }
 
 public class MyContainerClass : MonoBehavior, ISerializationCallbackReceiver {
     public MyPlainClass[] myArray;
 
     public void OnBeforeSerialize(){
         for (int i=0; i<myArray.Length; ++i){
             if (!myArray[i].isInitialized) myArray[i] = new MyPlainClass();
         }
     }
 }
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

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

Is it possible to customize how a variable of a certain type would look in the inspector? 1 Answer

Generate object diagram from unity editor 0 Answers

Hierarchy disappears in Unity 5.2 from 4.6.8 1 Answer

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Change 3rd person controller jump 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