• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Invalid iteration - (You need to stop calling Next when it returns false) 2 Answers

Get "this" SerializedProperty ? 1 Answer

Editor: How to do PropertyField for List elements? 4 Answers

Call a function from CustomPropertyDrawer of Arbitrary class 1 Answer

How do i stop my Editor window values from reseting? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges