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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by thrmotta · Mar 26, 2015 at 05:06 PM · c#editorserializedpropertycustomeditorcaneditmultipleobjects

Editor Targets are updated but SerializedPropertys arent

Hey everyone!

Im building an application where I need to have Multiple Objects Edition while having a custom interface, so Im creating some Editor classes. Right now Im trying to update each selected object with a foreach target and then use it somewhere else with a SerializedProperty var, but this is just not working how I thought it should be.

_thrustersProp is defined as follows:

 SerializedProperty _thrustersProp;

And inited inside OnEnable:

 _thrustersProp = serializedObject.FindProperty ("_thrusters");

This is the code to update all selected targets, found within the ThrustersEditor class:

 if( InterfaceEditor.CreateField( _thrustersCountProp, "SetThrusters" ) )
 {
     Debug.Log ( "updating" );
     foreach ( SimVrRobotics.ROV.Thrusters pm in targets )
     {
         Debug.Log ( "Before - pm._thrusters.Length: " + pm._thrusters.Length );
         pm.updateThrusters( _thrustersCountProp.intValue );
         Debug.Log ( "After - pm._thrusters.Length: " + pm._thrusters.Length );
     }
     serializedObject.ApplyModifiedProperties ();
     serializedObject.Update ();
 
     Debug.Log("A - _thrustersProp.size: " + _thrustersProp.arraySize);
 }

 

When Debugging, I can see that the "After" Debug shows me the behaviour Id expect, but this doesnt happen on the "A - " Debug, which shows me size 0.

This is the updateThrusters method, found within the Thrusters class, and here both Debugs works as expected.

 public void updateThrusters( int newThrustersCount )
 {   
     Thruster[] newThrusters = new Thruster[newThrustersCount];
 
     int previousThrusters = 0;
     Debug.Log( "newThrustersCount: " + newThrustersCount + " _thrusters.Length: " + _thrusters.Length );
     for( ; previousThrusters < _thrusters.Length; previousThrusters++ )
     {
         newThrusters[previousThrusters].SetRotation     ( _thrusters[previousThrusters].GetRotation()      );
         newThrusters[previousThrusters].SetRelativePos  ( _thrusters[previousThrusters].GetRelativePos()   );
         newThrusters[previousThrusters].SetMaxEffort    ( _thrusters[previousThrusters].GetMaxEffort()     );
         newThrusters[previousThrusters].SetSetThruster  ( _thrusters[previousThrusters].GetSetThruster()   );
         newThrusters[previousThrusters].SetFoldout      ( _thrusters[previousThrusters].GetFoldout()       );
     }
     if ( _thrusters.Length < newThrustersCount )
     {
         for ( ; previousThrusters < newThrustersCount; previousThrusters++ )
         {
             newThrusters[previousThrusters] = new Thruster();
         }
     }
 
     
     _thrustersCount = newThrustersCount;
     System.Array.Clear( _thrusters, 0, _thrusters.Length );
     _thrusters = new Thruster[newThrustersCount];
     newThrusters.CopyTo( _thrusters, 0 );
     Debug.Log( "2newThrustersCount: " + newThrustersCount + " _thrusters.Length: " + _thrusters.Length );
 }
 
 }

This is the nested class Thruster, defined inside the Thrusters class:

 [System.Serializable]
 public class Thruster
 {
     public bool    _setThruster;
     public Vector3 _relativePos;
     public Vector3 _rotation;
     public Vector3 _direction;
     public float   _maxEffort;
     public bool    _foldout;
 
     public Thruster ( )
     {
         _setThruster = false;;
         _relativePos = new Vector3(0f, 0f, 0f);
         _rotation    = new Vector3(0f, 0f, 0f);
         _direction   = new Vector3(0f, 0f, 0f);
         _maxEffort   = 0.0f;
         _foldout = false;
 
     }
 }

And finally, the following snip method, defined within the ThrustersEditor class, is how I access the nested class Thruster and where I get the error "Retrieving array element that was out of bounds", because it has an arraySize of 0 according to Debug.Log.

 private void createAttributes(  )
 {
     for( int i = 0; i < _thrustersCountProp.intValue; i++ )
     {                
         Debug.Log ( "createAttributes - i: " + i + " _thrustersProp.size: " + _thrustersProp.arraySize);
         SerializedProperty thrusterElementProp = _thrustersProp.GetArrayElementAtIndex( i );
         
         SerializedProperty setThrusterProp     = thrusterElementProp.FindPropertyRelative( "_setThruster"     );
         SerializedProperty relativePosProp  = thrusterElementProp.FindPropertyRelative( "_relativePos"     );
         SerializedProperty rotationProp     = thrusterElementProp.FindPropertyRelative( "_rotation"     );
         SerializedProperty directionProp     = thrusterElementProp.FindPropertyRelative( "_direction"     );
         SerializedProperty maxEffortProp     = thrusterElementProp.FindPropertyRelative( "_maxEffort"     );
         SerializedProperty foldoutProp        = thrusterElementProp.FindPropertyRelative( "_foldout"         );
 
         EditorGUILayout.Space();
         InterfaceEditor.CreateBoldFoldout( "Thruster", foldoutProp, " " + i, true );
         EditorGUI.indentLevel++;
     }
 }

What am I missing here?

Thank you for reading till the end and sorry for the long post, I just wanted to give as much information needed.

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

1 Reply

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

Answer by thrmotta · Mar 26, 2015 at 06:37 PM

I dont understand why, but apparently I cant update an array the way I was. The way that worked for me was simply substituting the foreach where the update occured with _thrustersProp.arraySize = _thrustersCountProp.intValue;

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

20 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

Related Questions

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

How do you actually use CanEditMultipleObjects 1 Answer

Get "this" SerializedProperty ? 1 Answer

Multiple Cars not working 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