After assigned to SerializedProperty.objectReferenceValue it is still null

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.

I’m on the same boat with you when I try to create a ConditionCollection.asset in the Adventure Tutorial. And the objectReferenceValue comes to null forever. I found an answer seems to be related to this situation —— objectReferenceValue in SerializedProperty - Questions & Answers - Unity Discussions —— Yet I’m not sure what the answer exactly mean due to my poor English and understanding ability, hope it’ll be useful to you.Wish someone could help us solve the problem : )

If your Level ScriptableObject is an asset (i.e., a file visible in your Project view), you’ll need to add newWave to the asset as a sub-asset using AssetDatabase.AddObjectToAsset().

1 Like

Yeah, it does work! I found this method exists in the AllConditionEditor Class in the tutorial like below:

 private void AddCondition(string description)
    {
        if (!AllConditions.Instance)
        {
            Debug.LogError("AllConditions has not been created yet.");
            return;
        }

        Condition newCondition = ConditionEditor.CreateCondition (description);
        newCondition.name = description;

        Undo.RecordObject(newCondition, "Created new Condition");

        AssetDatabase.AddObjectToAsset(newCondition, AllConditions.Instance);
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newCondition));

        ArrayUtility.Add(ref AllConditions.Instance.conditions, newCondition);

        EditorUtility.SetDirty(AllConditions.Instance);

        SetAllConditionDescriptions ();
    }

Additionally it add AssetDatabase.ImportAsset() method, could you tell me why it add this method and I’ll be very appreciated!

After you add objects to assets (or create new assets), you need to call ImportAsset on the path to the asset for the new object to show up in your project view. There might be other things that ImportAsset enables, I’m not sure.

1 Like