using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemySpawner : MonoBehaviour
{
[SerializeField]
private float spawnRadius = 7, time = 1.5f;
public GameObject[] enemies;
void Start()
{
}
// Update is called once per frame
void Update()
{
StartCoroutine(SpawnAnEnemy());
}
IEnumerator SpawnAnEnemy()
{
Vector2 spawnPos = GameObject.Find("Player").transform.position;
spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity);
yield return new WaitForSeconds(time);
StartCoroutine(SpawnAnEnemy());
}
}
I don’t get any errors
I have already tried changing the time value
Because your Coroutine is in the Update function, which is called every frame. Put that in your Start function
1 Like
thank you i was also wondering how i delete a thread?
You’re calling your coroutine in Update. Remember that Update runs every single frame (60+ times per second), so that means you’ll spawn a monster every frame at first. Then each of your coroutines will start more coroutines, so soon you’ll be spawning more than one per frame. Try starting your coroutine from Start, which only runs once.
Threads don’t get deleted. they stay up to help other people who may have similar questions.
ok thx