#SerialzedObjects

1 messages · Page 1 of 1 (latest)

rapid ridge
#

It would be something like this

for(int i = 0; i < charactersProperty.arraySize; i++)
{
    using (var charSO = new SerializedObject(charactersProperty.GetArrayElementAt(i).objectReferenceValue)
    {
      // draw the fields of the charSO that you want.
    }
}
green gulch
#

and "using" allows me to get each of the values individually

rapid ridge
green gulch
#

okay that makes sense

#

so can i just access the character's parameters by using the member variables

green gulch
#

wait

#

sorry

#

i meant can i just access the member variables directly

#

and display them individually

rapid ridge
#

charactersProperty.GetArrayElementAt(0).objectReferenceValue is the same as characters[0] basically

#

But then to access the properties of that UnityEngine.Object you need to make a SerializedObject for it

#

@green gulch

// Get the character property at index 0.
SerializedProperty characterArrayProp = charactersProperty.GetArrayElementAt(0);

// Create a SerializedObject for the value of the property we just got.
var charSO = new new SerializedObject(characterArrayProp.objectReferenceValue);

// Get the foo property from the character object.
SerializedProperty fooProp = charSO.FindProperty("foo");
// Set the value of the property.
fooProp.intValue = 42;

// Apply the changed value to the character.
charSO.ApplyModifiedProperties();
#

This is the equivalent

characters[0].foo = 42;
green gulch
#

okay, i think i get it, i'll let you know if i have more questions

tawdry badge
#

No

green gulch
rapid ridge
#

Also are there ay errors

green gulch
#

Yes

#

There are elements

rapid ridge
#

Are you using C# properties and [SerializeField] with private fields, or public fields?

green gulch
#

Oh I’m getting one error

#

It says on line 29 that object reference is not set to instance so I just forgot to name it

#

Oh wait

#

Not even that I don’t even have a name variable I just had a brain fart

#

Thanks, everything works now

green gulch
#

I was wrong, some things still aren't working. Only the name gets displayed in the inspector, and only one of the characters will show up, not more than one. Also I can't actually update anything

#

here's what the script looks like now.

rapid ridge
#

Also are you using C# properties and [SerializeField] with private fields, or public fields?

#

For example, what is Health?

green gulch
#

public

rapid ridge
green gulch
#

yes

green gulch
rapid ridge
#

like moveSpeed.floatValue = EditorGUILayout.FloatField("Move Speed", moveSpeed.floatValue);

#

Would just be EditorGUILayout.PropertyField(moveSpeed);