- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
PDZ · May 15, 2014 at 09:39 AM ·
editoreditor-scriptingserializationcustom-inspector
Custom Editor for Monobehavior with custom property not retaining Gameobject references
I am writing my own custom editor for a gameobject
[Serializable]
public class GameObjectPODO {
[SerializeField]
public GameObject Model;
[SerializeField]
public string DisplayName;
public GameObjectPODO(GameObject theModel, string displayName) {
theModel = Model;
DisplayName = displayName;
}
}
public class ObjectViewer : MonoBehaviour {
/// <summary>
/// The models that are hooked up to the system
/// </summary>
[SerializeField]
public List<GameObjectPODO> Models = new List<GameObject>();
}
In the custom editor, I click and drag a gameobject into a property field and it gets added to the system as follows
GameObject newModel = null;
newModel = EditorGUILayout.ObjectField ("Add New Model", newModel, typeof(GameObject), false) as GameObject;
//if they dragged a model in and it isn't contained in the previewer, add it
if (newModel != null) {
GameObjectPODO newModelObj = new GameObjectPODO(newModel, newModel.name); //PROBLEM HERE
_previewer.Models.Add(newModelObj);
}
But the gameobject reference that I make with the new instance of GameObjectPODO gets lost and is turned null after the object is created.
Can anyone shed some light on this?
Thanks in advance
Comment
Your answer
Follow this Question
Related Questions
Custom inspector variable resets automatically 1 Answer
How to access Custom Inspector own gameObject 1 Answer
How to assign object reference to ObjectField knowing the object's name? 2 Answers
Dynamic serialized fields based on enum 0 Answers
OnInspectorGUI changes reset when played in editor or building 2 Answers