- Home /
A warning before a enemy spawns
Hello, I have randomly spawnend circles and I want that before they are spawned that there is a warning or something like that at the same position as the circle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircleSpawn : MonoBehaviour
{
public GameObject Kreis;
float randX;
float randY;
Vector2 Spawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
void Start()
{
}
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-15.16f, 15.16f);
randY = Random.Range(-6.82f, 6.82f);
Spawn = new Vector2(randX, randY);
GameObject KreisKaputt = Instantiate( Kreis, Spawn, Quaternion.identity) ;
Destroy(KreisKaputt, 10f);
}
}
}
Sry for the bad english ^^ Thx for all answers :) `
I would just use coroutines for that instead, it would be cleaner, easier to read and debug
void Start()
{
StartCoroutine(Spawning());
}
Ienumerator Spawning()
{
while(true)
{
// do any warning thing
yield return new WaitForSeconds(1f) // stops your further code executing until the time in brackets passes
randX = Random.Range(-15.16f, 15.16f);
randY = Random.Range(-6.82f, 6.82f);
Spawn = new Vector2(randX, randY);
GameObject KreisKaputt = Instantiate( Kreis, Spawn, Quaternion.identity) ;
Destroy(KreisKaputt, 10f);
}
}
Answer by Captain-Ravioli · Jul 22, 2020 at 10:12 AM
Add [SerializeField] ParticleSystem spawnEffect;
Extract the code in Update()
to a new function, and make sure to add IEnumerator
instead of void
or whatever is set there.
Make a new line after line 27, and put: var spawnParticles = Instantiate(spawnEffect, Spawn, Quaternion.identity); yield return new WaitForSeconds(time desired); Destroy(spawnParticles);
After these 3 lines, put the rest of your code.
Make sure to customize the particle system I sent so that it suits your game.
Your answer
Follow this Question
Related Questions
Photon Spawn on server, not client 0 Answers
enemy spawning when dead 1 Answer
Spawning enemies javascript 2 Answers
Enemy Spawning 2 Answers
Trying to make a spawning enemy code 1 Answer