I have checked to see if I am asking a duplicate question. While I may have found one question that seems the same as mine, it doesn’t actually answer my version of it. (ref: How do I call a method from another script without creating another instance of it? - Questions & Answers - Unity Discussions )
So I have an object that has a generic wave spawner script on it. and each enemy that gets spawned has its own behaviour script attached to it. I want to calculate the enemies health, based on the wave_number variable in the EnemySpawner.cs
EnemySpawner.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public enum SpawnState { SPAWNING, WAITING, COUNTING};
public Transform enemy;
public int count;
public float rate;
private int wave_number;
public float time_between_waves;
private float wave_countdown;
private float search_countdown;
public SpawnState state;
void Start()
{
wave_number = 0;
wave_countdown = time_between_waves;
search_countdown = 1.0f;
state = SpawnState.COUNTING;
}
void Update()
{
if (state == SpawnState.WAITING)
{
if (AllEnemiesAreDead())
{
WaveCompleted();
}
}
// Everything else within this scope is not important
}
void WaveCompleted()
{
wave_number++;
Debug.Log("wave number: " + (wave_number));
state = SpawnState.COUNTING;
wave_countdown = time_between_waves;
}
bool AllEnemiesAreDead()
{
// Not important
}
private IEnumerator SpawnWave()
{
// Not important
}
private void SpawnEnemy()
{
Instantiate(enemy, transform.position, transform.rotation);
}
public int GetSpawnedEnemyHealth()
{
return (100 * (wave_number + 1));
}
}
Now below is the script attached to each of the enemies. when they are spawned, the Start() method tries to call GetSpawnedEnemyHealth() from the EnemySpawner script. But I am always getting 100. I am fairly confident that this is because I am creating an instance of the Spawner script in each of the enemy behaviour scripts.
EnemyBehaviour.cs
public class EnemyBehaviour : MonoBehaviour
{
public int health;
public GameObject enemy_spawner_script;
void Start()
{
health = enemy_spawner_script.GetComponent<EnemySpawner>().GetSpawnedEnemyHealth();
I wanted to post this on here to see if anyone can either tell me I’m wrong and that its not working for a different reason, or how I can call a method from another script without making a new instance of it. (i feel like the answer has something to do with being static, but that’s all I got so far). Thanks in advance