how to make a enemy spawn in a specific place? 2d c#
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 tutorial explains it pretty simply. Or just copy the code from it and work it out. InvokeRepeating isn't the best method to use but it is a good starting point before developing anything more complex.
Answer by Zoogyburger · Apr 09, 2016 at 10:21 PM
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);
}
}
Answer by ChaosEaterY · Oct 22, 2021 at 09:34 AM
{ 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;
}
}
}
Answer by EvilBob99 · Oct 26, 2021 at 09:15 AM
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
Your answer
Follow this Question
Related Questions
Spawning Bug. 1 Answer
NetworkServer.SpawnWithClientAuthority not working? 1 Answer
How to drop a box 0 Answers
error CS1526: A new expression requires () or [] after type 1 Answer
[uNET] Network objects spawned not spawn in new clients 1 Answer