Script Not Working With Unity Event

Hi all. I’ve got an issue when I’m using a Unity Event (the one that you use via the inspector in the editor) to call a method in a script.
My script continually spawns enemies up to some max value, and each time an enemy dies the EnemyDeath() function is called by a Unity Event in order to decrement the currentEnemies variable. The problem is that, for some reason, when the event is called, it’s like my script is being instantiated, as all of its variables are reset to their default values JUST for the EnemyDeath() function call.

Script:

public class EnemySpawner : MonoBehaviour
{
    public GameObject skeletonEnemy;
    public int maxEnemies;
    int currentEnemeies;
    float timer = 10f;
    public float spawnTimeMultiplier;
    float timeBetweenSpawns;
    Vector3 playerPosition;
    public bool spawnEnemies;
    public Vector2 maxLevelBoundies;
    public Vector2 minLevelBoundaries;
    Vector3 spawnPosition = new();

    private void OnEnable()
    {
       
        CalcTimerBetweenSpawns();
        timer = timeBetweenSpawns;
    }

    private void Update()
    {
       
        if (currentEnemeies >= maxEnemies || !spawnEnemies) { return; }
        if (timer > 0f)
        {
            timer -= Time.deltaTime;
           
        }
        else
        {
            timer = timeBetweenSpawns;
            SpawnEnemy(skeletonEnemy);
        }

    }

    public void SpawnEnemy(GameObject enemy)
    {
        if (playerPosition == null)
        {
            playerPosition = GameObject.Find("Player").GetComponent<Transform>().position;
        }
        SpawnPositionCalculator();
        GameObject.Instantiate(enemy, spawnPosition, Quaternion.identity);
        currentEnemeies++;
        CalcTimerBetweenSpawns();
    }

    public void EnemyDeath()
    {
        Debug.Log("FeedBack from EnemySpawner script.");
        if (currentEnemeies > 0)
        {
            currentEnemeies--;
            CalcTimerBetweenSpawns();
           
        }
    }

    void SpawnPositionCalculator()
    {
        spawnPosition.x = Random.Range(minLevelBoundaries.x + 1, maxLevelBoundies.x - 1);
        spawnPosition.z = Random.Range(minLevelBoundaries.y + 1, maxLevelBoundies.y - 1); //Position.z is correct. Not position.y
        spawnPosition.y = playerPosition.y;
    }

    void CalcTimerBetweenSpawns()
    {
        timeBetweenSpawns = (5f / (5f + maxEnemies - currentEnemeies)) * spawnTimeMultiplier;
    }

}

Using Visual Studio, I place a break point at the first if statement in the Update() function and another breakpoint at the if statement in the EnemyDeath() function. When I’m viewing the break point in the Update() function everything looks fine. CurrentEnemies is incremented each time an enemy is spawned. But when it jumps tot he break point in the EnemyDeath() function all of the variables are reset to default. CurrentEnemies is zero, timeBetweenSpawns is zero, spawnPosition is zero, and timer is 10 (all default values).

Jumping back to the first break point, everything is again as it should be. I’m looking at the break point as I’m typing this and currentEnemies is 8, spawnPosition is (5.24,0,-1.65), timeBetweenSpawns is 0.714, and timer is 0.0416.

What’s going on here?

Note that the Unity Event is on an enemy prefab and the EnemySpawner script is on a separate prefab. Might this have something to do with it?

I can’t tell, because I can’t verify if you have everything in the scene setup correctly. But, if your Spawner is in the scene and your prefab is in the project folder, you most likely don’t have them connected correctly. Chances are, you need to setup the event connection after you spawn the enemy. Otherwise, you’re probably not targeting the instance of your spawner that is in the scene.

1 Like