- Home /
Instantiated objects into serialized property?
So, I have a custom object. Let's call it Template. Within the Template, I have a list containing Actions. I have a few derived Action classes, let's say they're called Celebrate, Party, and Dance.
I've got my list of Actions set as a SerializedProperty, and I can select which derived class I want to use with a few buttons I've made. The problem is, I can't figure out how to instantiate an Action object and add it to the list.
Anyone have any insight?
Also note, I do have to rely on inheritance for Actions, though I'm not picky about the root type that the Action class derives from.
Here's one of a few things I've tried. Debugging shows that the array slot is being filled with something so it's no longer null, but I can't access any variables whatsoever, and using oAction.ToString () yields a blank string:
SerializedProperty oActionList = serializedObject.FindPropertyRelative ("actions");
Action oNewAction = Object.Instantiate (actions[(int)oAction]) as Action;
oActionList.arraySize++;
oActionList.GetArrayElementAtIndex
(oActionList.arraySize-1).objectReferenceValue = oNewAction;
serializedObject.ApplyModifiedProperties ();
Hm... I'll see if I can isolate the relevant lines. The rest of it is a bit of a mess.
INCO$$anonymous$$ING NEWS THEY DONT CARE (if you bought the game if not they will say something along the lines of...)
Hi that must suck, dont know why, how bout u go buy my game an get me a raise
Answer by Nemox · Feb 15, 2015 at 05:00 AM
Figured it out! Turns out to add the Action into the serialized array, it had to be in the form of a ScriptableObject or else it could not be of a derived class.
Then, because it was temporary data, it merely disappeared when the function ended and was cleaned up.
Instead, I had to add the Action to the Template's asset file, then set the array slot's reference ID to the new action's ID, in that order.
AssetDatabase.AddObjectToAsset (oNewAction, Selection.activeObject);
oActionList.arraySize++;
oActionList.GetArrayElementAtIndex
(oActionList.arraySize-1).objectReferenceInstanceIDValue = oNewAction.GetInstanceID ();
Answer by nathanielstevens · Feb 14, 2015 at 05:58 AM
It's a little hard to tell with your snippet of code, but my first guess would be to look into having your Action class inherit from ScriptableObject and then you would use CreateInstance(). You can also you use the [System.Serializable] attribute on the Action class and just use the 'new' keyword (depending on what kind of data you're trying to serialize). I won't go into any more detail in case you've already checked out the avenues but please let me know if you need more information or I'm taking you down a path you've already been on.
http://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html http://docs.unity3d.com/ScriptReference/Serializable.html
Tried it with CreateInstance as well. No matter what form the object is in, it doesn't seem to be getting saved.
I've debugged it a little, and it seems that the serialized Template's action array items are not null after all, but they still contain no data whatsoever. I can't access any variables, and .ToString() yields nothing.
Could you post the code for the Action class? It may have to do with the data types it's using (This serialization stuff can be a real bear. I've spent the last week struggling with it myself).
[System.Serializable]
public class Action : ScriptableObject {
public string testString = "Test.";
}
The derived classes have no variables of their own yet.
What does Template inherit from? And when you say you can't access any variables, do you mean it's returning null or that there's some sort of error?
Template is just a ScriptableObject. When I go back and serialize the list of actions, I check whether each action == null. The test shows that they aren't null. But when I try to access anything like action.testString, it gives me a null reference exception error.
Your answer
