title: Unity2D: instantiating multiple prefabs
body: <p>I have a composite prefab with multiple sprites and a script attached to it. The problem is, when I instantiate it more than once, the instances go crazy:</p>
  
  <ol>
  <li><p>Their position on the screen is partly OK and partly determined by the prefab's setup in the editor. But Debug.Log reveals that the transform.position variable is set to the same value for all the instances. (It's the value that was used by the last instantiation.)</p></li>
  <li><p>Their script, when accessed by GetComponent&lt;>, behaves strangely. It doesn't access some members correctly. (Complicated to define.)</p></li>
  </ol>
  
  <p>Here is a function that instantiates the prefab:</p>
  
  <pre><code>private GameObject SpawnEntity(GameObject p, Vector2 coords)
      {
          Instantiate(p);
          ...
          p.transform.position = coords;
          return p;
      }
  </code></pre>
  
  <p>It is called like this:</p>
  
  <pre><code>public GameObject humanoid;
  ...
  SpawnEntity(humanoid, new Vector2(-1,-3));
  </code></pre>
  
  <p>The Unity version is 2018.2.1f1.</p>