- Home /
field become null after object as instantiated using EditorGUILayout.ObjectField
Hello everyone, this is my code for change text to a label in the hierarchy:
public class Collectable : MonoBehaviour {
public UILabel HUDLabelOwner; //label of NGUI
void Start()
{
HUDLabelOwner.text = "This is a label"
}
}
[CustomEditor(typeof(Collectable))]
public class MyScriptEditor : Editor
{
public override void OnInspectorGUI()
{
Collectable cll = (Collectable)target;
cll.HUDLabelOwner = EditorGUILayout.ObjectField("HUDLabelOwner: ", cll.HUDLabelOwner, typeof(UILabel), true) as UILabel;
}
}
The gameobject that bring this script is a prefab, and is instantiated when I press play, while the label is already instantiated. I assign at prefab field the label by drag in from hierarchy, but when i press play all field are reset to null(prefab as well).
What can be? ?:\
i add inside $$anonymous$$yScriptEditor class base.OnInspectorGUI(); for see all fields in inspector and when i assign gameobject with UILabel attached in the original field(public UILabel HUDLabelOwner) appear "Type mismatch"....mmmmmh...
Answer by PixelFireXY · Aug 15, 2014 at 01:01 PM
Found a solution... I cant assign an instantiated gameobject in the hierarchy into a prefab's field :(( http://forum.unity3d.com/threads/mesh-type-mismatch-in-inspector-for-prefab.151437/
also i find this, for make this at runtime... http://answers.unity3d.com/questions/387052/custominspector-add-scriptableobject-in-a-list-alw.html
but i think that im gonna find another way.
Your answer
Follow this Question
Related Questions
Prevent Reset() from clearing out serialized fields 2 Answers
Set gameobject to null in a function 1 Answer
Component OnAwake or OnEnable with PropertyAttribute 1 Answer
Objects created in editor tool, fields reset on play and unable to serialize. 2 Answers
transform is destroyed (ERROR)... and I want the game to complete 1 Answer