Search Unity

Script is broken for prefabs, workaround: how do I make it read only?

Discussion in 'Editor & General Support' started by ematsuno, Oct 23, 2019.

  1. ematsuno

    ematsuno

    Joined:
    Oct 13, 2015
    Posts:
    27
    I have a script that generates and places objects
    If the user increases or decreases the number of objects in the editor, which is fine for editing, then it works fine. If it has been turned into a prefab, all hell breaks loose.


    The best solution, for now, is to make it read-only. so I have that question also:

    How do I make a read-only prefab? Or more specifically would I make a field dynamically rendered as a Read Only Field conditional to its being in the Editor and it's a Prefab. (not in run mode)?
     
  2. ematsuno

    ematsuno

    Joined:
    Oct 13, 2015
    Posts:
    27
    Aha, Found the answer to the latter question: Here it is:
    public override void OnInspectorGUI()
    {
    SomeClass flower = target as SomeClass ;

    if (PrefabUtility.GetPrefabAssetType(target) == PrefabAssetType.NotAPrefab)
    {
    base.OnInspectorGUI();
    }
    else
    {
    MonoScript script = MonoScript.FromMonoBehaviour(target as MonoBehaviour);
    EditorGUI.BeginDisabledGroup(true);
    EditorGUILayout.ObjectField("Script", script, typeof(MonoScript), false);
    EditorGUI.EndDisabledGroup();
    }
    }