title: How to edit a MonoBehaviour's properties on a prefab in the UnityEditor from Script
body: <p>Assume I have a simple <code>MonoBehaviour</code></p>
  
  <pre><code>public class FooScript : MonoBehaviour {
    public int someValue = 0;
  }
  </code></pre>
  
  <p>I make GameObject, attach a <code>FooScript</code>. I make a prefab from that and delete in instance in the scene.</p>
  
  <p>In the editor I can select the prefab in the project and I can edit <code>someValue</code>. </p>
  
  <p>Assuming I know the path to the prefab how I can make an edit time script that makes edits to the <code>FooScript</code> instance inside the prefab?</p>
  
  <p>What've tried</p>
  
  <pre><code>GameObject prefab = AssetDatabase.LoadAssetAtPath&lt;GameObject&gt;(pathToPrefab);
  GameObject root = ???? // WHAT DO I CALL HERE!?
  FooScript fooScript = root.GetComponent&lt;FooScript&gt;();
  fooScript.someValue = 789;
  </code></pre>
  
  <p>Actually in that case <code>prefab</code> is null</p>
  
  <p>If switch to</p>
  
  <pre><code>Object prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath);                 
  GameObject root = prefab as GameObject;
  </code></pre>
  
  <p><code>prefab</code> is not null but <code>root</code> is</p>
  
  <p>Next I tried this</p>
  
  <pre><code>Object[] objs = AssetDatabase.LoadAllAssetsAtPath(prefabPath);
  foreach (var o in obs)
  {
     Debug.Log("    type:" + (o.GetType().Name));
  }
  </code></pre>
  
  <p>That prints</p>
  
  <pre><code>type: GameObject
  type: Transform
  type: FooScript
  </code></pre>
  
  <p>But using that is a hacky solution as I'd have to try every GameObject and or FooScript. I means I suppose as a pragmatic solution it might work but it feels like adding technical debt I'll have to fix later    </p>
  
  <p>Other things I've tried is creating a new prefab with a new GameObject and a new FooScript and calling <code>PrefabUtility.ReplacePrefab</code></p>
  
  <pre><code>GameObject newGameObject = new GameObject();
  FooScript fooScript = newGameObject.AddComponent&lt;FooScript&gt;();
  fooScript.someValue = 789;
  
  Object prefab = AssetDatabase.LoadAssetAtPath&lt;GameObject&gt;(prefabPath);
  PrefabUtility.ReplacePrefab(newGameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
  
  GameObject.Destory(newGameObject); // don't want this in the scene
  </code></pre>
  
  <p>But that's not working for me. The values in the prefab get replaced but all instances in the scene get disconnected.</p>
  
  <p>To reiterate. I'm NOT trying to edit an instance of a prefab. I'm trying to edit the prefab itself from a script at edit time.</p>