title: How do I make my cloned game objects global, so I can use them outside of the function in Unity?
body: <p>I just recently got into Unity 3D and currently working on one of my first own projects. For the game im making, I need a spawner function, that respawns a clone of the enemy as soon as the enemy falls off the platform. This is the code I have right now:</p>
  
  <pre><code>using UnityEngine;
  
  public class spawner : MonoBehaviour
  {
      public GameObject enemyPrefab;
      public float spawnHeight = 0.75f;
  
      // Start is called before the first frame update
      void Start()
      {
          spawnEnemy();
      }
  
      // Update is called once per frame
      void Update()
      {
          if (enemyClone.transform.position.y &lt; -10)
          {
              Destroy(enemyClone);
              spawnEnemy();
          }
      }
  
      public void spawnEnemy()
      {
          var enemyPosition = new Vector3(Random.Range(-5, 5), spawnHeight, Random.Range(-5, 5));
          var enemyClone = Instantiate(enemyPrefab, enemyPosition, Quaternion.identity);
      }
  }
  </code></pre>
  
  <p>The function spawnEnemy itself works fine, since it creates an enemy on game start, tho further enemies aren't spawned. I get the message: "Assets\spawner.cs(21,21): error CS0103: The name 'enemyClone' does not exist in the current context".</p>
  
  <p>I do see why I get the message, don't know how to make enemyClone globally available however.</p>
  
  <p>Thanks to everybody in advance,</p>
  
  <p>bezunyl</p>