How do I change an objects script variable in my scene?

Wasn’t sure how to phrase the question properly, but basically I’m creating a wave spawner, and each time I spawn a new enemy, I increment a variable called “enemyAmount” by one.

I want this variable so that I can spawn a new wave when the spawner is no longer spawning enemies and there all enemies are killed.

The problem right now is that when the enemy dies, it only changes the prefabs enemyAmount by one, and I want it to change the enemyAmount of the empty spawner object in the scene, but it just keeps staying the same (except when an enemy is spawned).

So how do I change this object in the scene, so that when my enemy dies, the spawners enemyAmount variable in the scene is changed?

Here’s my code for the two scripts:

using UnityEngine;
using System.Collections;

public class Die : MonoBehaviour {

    public int health = 50;
    public GameObject explosion;
    public GameObject spawn;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
        if (health <= 0)
        {
            die();
        }
	}

    void die()
    {
        Instantiate(explosion, transform.position, Quaternion.Euler(0, 0, 0));
        Destroy(gameObject);
        spawn.GetComponent<Spawner>().removeEnemy();
    }
}

Spawner class:

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {

    public GameObject enemy;
    public int enemyAmount;
    public bool isSpawning = false;
    public int defaultSpawnNum = 5;
    public int waitTime = 1;
    public int waveNum = 1;

	// Use this for initialization
	void Start () {
        enemyAmount = 0;
        StartCoroutine(testSpawn(waveNum));
    }
	
	// Update is called once per frame
	void Update () {


        if(!isSpawning && enemyAmount == 0)
        {
            waveIncrement();
        }
	
	}

    IEnumerator testSpawn(int wave)
    {
        int spawnNumber = (defaultSpawnNum + 5 * (wave - 1));
        isSpawning = true;

        for (int i = 0; i < spawnNumber; i++)
        {
            yield return new WaitForSeconds(waitTime);
            Instantiate(enemy, transform.position, transform.rotation);
            enemyAmount++;
        }
        yield return new WaitForSeconds(10);
        isSpawning = false;


    }

    public void removeEnemy()
    {
        enemyAmount--;
    }

    public void waveIncrement()
    {
        waveNum++;
        StartCoroutine(testSpawn(waveNum));
    }

    
}

If you need further clarification or any other input please comment :slight_smile:

Here’s what my prefab script settings look like:
Unity wouldnt upload my pictures for some reason so here’s the URLs

Die class:

Spawner:

I can fix it by using the GameObject.Find method by putting spawn = GameObject.Find(“Wave_spawn”); in my start method of my enemy death script.
I was wondering if there’s a better way to do this, as I can imagine calling this each time an enemy is spawned, and having to find this gameobject for each enemy that spawns will take up some memory.

Well, since you asked for other ways, you could use events. This way your Die object doesn’t need about the spawner at all and any script can hook up with the event where the object dies.

     for (int i = 0; i < spawnNumber; i++)
     {
         yield return new WaitForSeconds(waitTime);

         // Get the Die component, and register a callback to the event onDeath
         var go = (GameObject)Instantiate(enemy, transform.position, transform.rotation);
         go.GetComponent<Die>().onDeath += removeEnemy;

         enemyAmount++;
     }

Then you add that event to Die…

public class Die : MonoBehaviour {
     public int health = 50;
     public GameObject explosion;

     // Declare an event onDeath
     public event System.Action onDeath;
     
     // ..... lots of code I dont want to copypaste for clarity ....
     
     void die()
     {
         Instantiate(explosion, transform.position, Quaternion.Euler(0, 0, 0));
         Destroy(gameObject);

         // Raise the onDeath event. This will call removeEnemy implicitly.
         if (onDeath != null) 
             onDeath();
     }

With this approach, you tell the Die.onDeath to call removeEnemy when it dies. Die calls all of the handlers on the onDeath event delegate.

If you don’t want to do it this way, then you could supply the spawn reference when you spawn the enemy:

         // Get the Die component, and set the spawn to this.
         var go = (GameObject)Instantiate(enemy, transform.position, transform.rotation);
         go.GetComponent<Die>().spawn = this;