Hello guys and galls I am a beginner and I wonder how make a enemy spawn in a specific place.
Right now i only have one enemy in a place moving towards me and when i hit it the game crashes because the code is searching for an enemy that does not exist, so i want it to spawn every few seconds.
please help me
This is my enemy spawner script
{
public float spawnTime = 5f;
//The amount of time between each spawn.
public float spawnDelay = 3f;
//The amount of time before spawning starts.
public GameObject[] enemies;
//Array of enemy prefabs.
public Vector3 enposition;
void Start ()
{
//Start calling the Spawn function repeatedly after a delay.
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
//Instantiate a random enemy.
int enemyIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[enemyIndex], enposition, transform.rotation);
}
}
{
public float SpawnDelay = 3f;
public GameObject Enemy;
public Vector3 SpawnPoint;
float timeSinceLastSpawn = Mathf.Infinity;
void Update()
{
timeSinceLastSpawn += Time.deltaTime
if (timeSinceLastSpawn >= SpawnDelay)
{
Instantiate(Enemy, SpawnPoint, Quaternion.identity);
timeSinceLastSpawn = 0;
}
}
}
ummm I think you would want your enemy to be a prefab you can spawn and just create an empty and create a new enemy from there every so often be cause based on your code that seems like what you want to do so find a way to set up a timer like maybe 5 seconds and have it go down when it reaches 0 a new enemy is spawned at the empty and it resets I know I haven’t helped very much but I’m trying to give you an idea of how to do this