Doubt regarding Unity3D Old Course

Hello everyone, I hope you are doing well.
Actually the code below is same as it is in the course but still output is very different, it is the EnemySpawner script of the game Realm Rush, in which we instantiate multiple enemies after a particular interval of time.

public class EnemySpawner : MonoBehaviour
{
[Range(0.1f, 120f)]
[SerializeField] float secondsBetweenSpawns = 2f;
[SerializeField] EnemyMovement enemyPrefab;

void Start()
{
    StartCoroutine(RepeatedlySpawnEnemies());
}

IEnumerator RepeatedlySpawnEnemies()
{
    while (true) // forever
    {
        print("Spawning");
        Instantiate(enemyPrefab, transform.position, Quaternion.identity);
        yield return new WaitForSeconds(secondsBetweenSpawns);
    }
}

}
And after playing it in unity what it does is very different and unexpected it makes multiple clones of the game object. I have attached a Screenshot of the output.


As you can see that there are many clones of the ‘Enemy’ game object so please explain me that why is this happening and what can I dot to solve this problem.

Hi Pratyush,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? What’s the value of secondsBetweenSpawns? Also make sure that there is only one EnemySpawner in your scene.


See also:

I don’t get it what do you mean by ‘Debug.Logs’.
And value of secondsBetweenSpawns is 3f in the script as well as in the inspector.
And yes there is only 1 EnemySpawner.

Ma’am please answer this this question its creating a big mess.
Please ma’am :pleading_face: :pleading_face: :pleading_face:

By Debug.Log, I mean this: Unity - Scripting API: Debug.Log

Don’t worry, I didn’t forget you. I just don’t work at the weekends. Actually.

If you are in a hurry, please feel free to ask our helpful community of students over on our Discord chat server.

I’m back on Monday. Guck luck and have a nice weekend! :slight_smile:

Greetings ma’am
I am very pleased to tell you that the error occuring in the game is solved.

Fantastic. Thanks for letting me know. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms