I’m creating a custom inspector which will hold many gameobject prefabs in objectfields for later instantiation. It is important that it is versatile so objectfields in the inspector are necessary. The object are able to be input and adjusted and everything works great until the game is started and stopped. Of course, because they are not properly serialized all of my gameobjects vanish from the objectfields in the inspector after the game is started and stopped. When I try to serialize them, unity throws this error “SerializationException: Type UnityEngine.GameObject is not marked as Serializable.” After some fiddling I’m not sure what else to try. Has anybody else run into this problem with their custom inspectors? And is there a quick (or not so quick) fix for this? What are some possible solutions to get these gameobjects to stay put? Any input would be appreciated! Thank you.
Here is a snippet from my inspector’s code handling the int and object fields… (info refers to the main script)
List<GameObject> GOlist;
List<int> Numlist;
if (!info.enemyDictionary.TryGetValue(currentKey, out GOlist))
{
GOlist = new List<GameObject>();
info.enemyDictionary.Add(currentKey, GOlist);
}
if (!info.numberDictionary.TryGetValue(currentKey, out Numlist))
{
Numlist = new List<int>();
info.numberDictionary.Add(currentKey, Numlist);
}
for (int i = 0; i < GOlist.Count; i++)
{
int ne = i+1;
GOlist _= EditorGUILayout.ObjectField("GO " + ne, GOlist*, typeof(GameObject), true) as GameObject;*_
* }*
* for (int i = 0; i < Numlist.Count; i++)*
* {*
* int ne = i+1;*
Numlist = EditorGUILayout.IntField("ID for GO " + ne, Numlist*);*
* }*
And here is a snippet from the main script holding the inputs and attempting to serialize them…
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[ExecuteInEditMode]
public class Controller : MonoBehaviour {
* public Dictionary<int,List> enemyDictionary = new Dictionary<int,List>();*
* public Dictionary<int,List> numberDictionary = new Dictionary<int,List>();*
* public void Save()*
* {*
* if (File.Exists(Application.persistentDataPath + “/MyScriptData.dat”))*
* {*
* BinaryFormatter bf = new BinaryFormatter();*
* FileStream file = File.Open(Application.persistentDataPath + “/MyScriptData.dat”, FileMode.Open);*
* MyScriptData data = (MyScriptData)bf.Deserialize(file);*
* data.enemyDictionary = enemyDictionary;*
* data.numberDictionary = numberDictionary;*
* bf.Serialize(file,data);*
* file.Close ();*
* }*
* else*
* {*
* BinaryFormatter bf = new BinaryFormatter();*
* FileStream file = File.Create(Application.persistentDataPath+ “/MyScriptData.dat”);*
* MyScriptData data = new MyScriptData();*
* data.enemyDictionary = enemyDictionary;*
* data.numberDictionary = numberDictionary;*
* bf.Serialize(file,data);*
* file.Close ();*
* }*
* }*
[System.Serializable]
class MyScriptData
{
* public Dictionary<int,List> enemyDictionary = new Dictionary<int,List>();*
* public Dictionary<int,List> numberDictionary = new Dictionary<int,List>();*
}
}