Hey All,
I need help understanding why this is happening
I have two scriptableObjects
public class Wave : ScriptableObject
{
public float Time;
public GameObject Enemy;
public GameObject Formation;
}
public class Level : ScriptableObject
{
public GameObject Boss;
public Wave[] Waves = new Wave[0];
}
In my custom editor, I want to add new Wave to my Waves array using the following method.
I used the code from Adventure Game Unity Tutorial - Phase 3 of 6 - Conditions
// Use this to add an object to an object array represented by a SerializedProperty.
public static void AddToObjectArray<T> (this SerializedProperty arrayProperty, T elementToAdd)
where T : Object
{
// If the SerializedProperty this is being called from is not an array, throw an exception.
if (!arrayProperty.isArray)
throw new UnityException("SerializedProperty " + arrayProperty.name + " is not an array.");
// Pull all the information from the target of the serializedObject.
arrayProperty.serializedObject.Update();
// Add a null array element to the end of the array then populate it with the object parameter.
arrayProperty.InsertArrayElementAtIndex(arrayProperty.arraySize);
arrayProperty.GetArrayElementAtIndex(arrayProperty.arraySize - 1).objectReferenceValue = elementToAdd;
// Push all the information on the serializedObject back to the target.
arrayProperty.serializedObject.ApplyModifiedProperties();
}
When I debug the code I can see that after the assignment the array in the last index is still null, why?
To create the new Wave I use:
Wave newWave = CreateInstance<Wave>();
And to add it to the array:
//getting the relevant fields from the serialized object
wavesProperty = serializedObject.FindProperty(levelPropWavesName);
.
.
.
wavesProperty.AddToObjectArray(newWave);
It is working in the example project… but not for me.
Update:
After some debugging and playing with the example project, its seems like the scriptable object (Level) must be part of a Game Object that exist in the scene to be able to create Waves.
But I’m not sure why…
Please help.