title: Unity Change sprite in sprite renderer in Runtime
body: <p>In unity I am using a script to randomly create enemies that attack your player. To do this I need to <strong><em>insert a game object</em></strong>, <strong><em>add a SpriteRenderer</em></strong>, <strong><em>name it</em></strong>, and then <strong><em>give it a texture</em></strong> which is in <strong>assets</strong>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>GameObject sprGameObj = new GameObject();
  sprGameObj.AddComponent&lt;SpriteRenderer&gt;();
  sprGameObj.name = "Enemy";
  
  sprGameObj.GetComponent&lt;SpriteRenderer&gt;().sprite = Resources.Load&lt;Sprite&gt;("EnemyImage");
  </code></pre>
  
  <p>The code inserts a game object, adds the sprite renderer and names it however does not add the image "EnemyImage" to the sprite. I have looked online but everyone else is using code similar to mine which doesn't work either. </p>
  
  <p>Does anyone know a solution?</p>
  
  <p><strong>I am coding in C#.</strong></p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>I tried putting this code inside an already existing sprite and it just removed it's image:</p>
  
  <pre><code>gameObject.GetComponent&lt;SpriteRenderer&gt;().sprite = Resources.Load&lt;Sprite&gt;("EnemyImage");
  </code></pre>
  
  <p>Could there be a problem with the image itself?</p>