0

I have a class, where my enemies spawn at the start of the game. When my player dies, I wanna call the spawn class again to let the enemies spawn again. But when I do so, nothing is happening.

public class EnemySpawn : MonoBehaviour
{

    public GameObject [] Enemies;
    public GameObject [] SpawnClone;
    public Transform [] locations;

    public void Start()
    {
       SpawnClone[0] = Instantiate(Enemies[0], locations[0].transform.position, Quaternion.Euler(0,0,0)) as GameObject; 
       SpawnClone[1] = Instantiate(Enemies[1], locations[1].transform.position, Quaternion.Euler(0,0,0)) as GameObject; 
       SpawnClone[2] = Instantiate(Enemies[2], locations[2].transform.position, Quaternion.Euler(0,0,0)) as GameObject;  
    }
    
}

//other class where I wanna call the Start method again to spawn enemies everytime I die
 private EnemySpawn enemyspawn;
 private void Die()
        {
            m_character.NotifyDied();

            if (m_canRespawn)
            {
                SetVulnerable();
                RemovePoison();
                m_hazards.Clear();
                gameObject.transform.position = m_spawnPosition;
                SetHealth(m_maxHealth);
                enemyspawn.Start();
            }
            else {
                Destroy(gameObject);
            }
        }
HappyJava
  • 19
  • 5
  • Like the Awake function, Start() is called exactly once in the lifetime of the script. Start is not to be called by another script. Make a public Init() function that is called by your Start in EnemySpawn and from Die. – Senbazuru Jul 18 '22 at 08:34

1 Answers1

0

Start method is reserved and is called on the frame when a script is enabled. You should use another method name.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

You can make it like this, where Spawn is called on Start and is also called on Die method

public class EnemySpawn : MonoBehaviour
{

    public GameObject [] Enemies;
    public GameObject [] SpawnClone;
    public Transform [] locations;

    public void Start()
    {
        Spawn();
    }

    public void Spawn()
    {
       SpawnClone[0] = Instantiate(Enemies[0], locations[0].transform.position, Quaternion.Euler(0,0,0)) as GameObject; 
       SpawnClone[1] = Instantiate(Enemies[1], locations[1].transform.position, Quaternion.Euler(0,0,0)) as GameObject; 
       SpawnClone[2] = Instantiate(Enemies[2], locations[2].transform.position, Quaternion.Euler(0,0,0)) as GameObject;
    }
    
}


public class EnemySpawnCaller : MonoBehaviour
{
    public EnemySpawn enemyspawn;

    private void Die()
        {
        
        enemyspawn.Spawn();

        }
}
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
  • When I use a method like SpawnEnemies() and try to call it in the Die() method, also nothing happens... – HappyJava Jul 18 '22 at 08:42
  • Did you add the reference in your other script to EnemySpawn in the editor? drag enemyspawn and drop it so it has a reference to it. – Milad Qasemi Jul 18 '22 at 08:44
  • I tried with your example and it worked at start. But when I die, they do not respawn. – HappyJava Jul 18 '22 at 08:47
  • First, check to see if the Die function is even being called or not. – Milad Qasemi Jul 18 '22 at 08:49
  • How do I do that? Sorry but I am really new to unity & c#. But I guess is getting called, since the player respawns with maxHealth etc. – HappyJava Jul 18 '22 at 08:52
  • I guess 'enemyspawn' is null. So make 'EnemySpawn enemyspawn;' field public and drag 'EnemySpawn' script and drop it into that field in editor so it has a reference to it. – Milad Qasemi Jul 18 '22 at 08:59
  • That worked, thank you so much!! I got one issue though. They respawn like they should, but when I dont kill them and die, they spawn on top of each other. Any ideas on how to fix that? – HappyJava Jul 18 '22 at 09:05