- Home /
how to stop Instantiate after 3 minuts,trying to stop Instantiate after 3 minuts
public class EnemySpawner : MonoBehaviour
{
public float SpawnRate;
public float SpawnStart;
public GameObject EnemyPrefab;
public float MaxSpeed;
public float MinSpeed;
Vector2 Min;
Vector2 Max;
public GameObject StopSpawning;
void Start()
{
Min = Camera.main.ViewportToWorldPoint(new Vector3(0, 0));
Max = Camera.main.ViewportToWorldPoint(new Vector3(1, 1));
InvokeRepeating("SpawnerEnemy", SpawnStart, SpawnRate);
}
void SpawnerEnemy(float v )
{
float ExtendsX = EnemyPrefab.GetComponent<Renderer>().bounds.extents.x;
float ExtendsY = EnemyPrefab.GetComponent<Renderer>().bounds.extents.y;
float RandomX = Random.Range(Min.x + ExtendsX, Max.x - ExtendsX);
Vector2 RandomPosition = new Vector2(RandomX, Max.y+ExtendsY);
GameObject Enemy = Instantiate(EnemyPrefab, RandomPosition, Quaternion.identity);
Enemy.GetComponent<EnemyController>().Speed = Random.Range(MinSpeed, MaxSpeed);
// StopSpawning = yield return new WaitForSeconds(180) ;
//if (Enemy.gameObject.active == false)
// {
// CancelInvoke("SpawnerEnemy");
// }
}
void Update()
{
}
}
, public class EnemySpawner : MonoBehaviour { public float SpawnRate; public float SpawnStart; public GameObject EnemyPrefab; public float MaxSpeed; public float MinSpeed; Vector2 Min; Vector2 Max; public GameObject StopSpawning;
void Start()
{
Min = Camera.main.ViewportToWorldPoint(new Vector3(0, 0));
Max = Camera.main.ViewportToWorldPoint(new Vector3(1, 1));
InvokeRepeating("SpawnerEnemy", SpawnStart, SpawnRate);
}
void SpawnerEnemy(float v )
{
float ExtendsX = EnemyPrefab.GetComponent<Renderer>().bounds.extents.x;
float ExtendsY = EnemyPrefab.GetComponent<Renderer>().bounds.extents.y;
float RandomX = Random.Range(Min.x + ExtendsX, Max.x - ExtendsX);
Vector2 RandomPosition = new Vector2(RandomX, Max.y+ExtendsY);
GameObject Enemy = Instantiate(EnemyPrefab, RandomPosition, Quaternion.identity);
Enemy.GetComponent<EnemyController>().Speed = Random.Range(MinSpeed, MaxSpeed);
// StopSpawning = yield return new WaitForSeconds(180) ;
//if (Enemy.gameObject.active == false)
// {
// CancelInvoke("SpawnerEnemy");
// }
}
void Update()
{
}
}
Answer by JackhammerGaming · Aug 03, 2020 at 02:36 PM
@strespro you can for sure use coroutine but i personally never use it so what you can do is -:
public class EnemySpawner : MonoBehaviour
{
public float SpawnRate;
public float SpawnStart;
public GameObject EnemyPrefab;
public float MaxSpeed;
public float MinSpeed;
Vector2 Min;
Vector2 Max;
public GameObject StopSpawning;
public float stop_time = 180f; // a new float declaration
void Start()
{
Min = Camera.main.ViewportToWorldPoint(new Vector3(0, 0));
Max = Camera.main.ViewportToWorldPoint(new Vector3(1, 1));
InvokeRepeating("SpawnerEnemy", SpawnStart, SpawnRate);
}
void SpawnerEnemy(float v )
{
float ExtendsX = EnemyPrefab.GetComponent<Renderer>().bounds.extents.x;
float ExtendsY = EnemyPrefab.GetComponent<Renderer>().bounds.extents.y;
float RandomX = Random.Range(Min.x + ExtendsX, Max.x - ExtendsX);
Vector2 RandomPosition = new Vector2(RandomX, Max.y+ExtendsY);
GameObject Enemy = Instantiate(EnemyPrefab, RandomPosition, Quaternion.identity);
Enemy.GetComponent<EnemyController>().Speed = Random.Range(MinSpeed, MaxSpeed);
}
void Update()
{
if (stop_time <= 0){
CancelInvoke("SpawnerEnemy");
}
else{
stop_time -= Time.deltaTime;
}
}
}
i am just subtracting time with Time.deltaTime which is the time taken to render each frame. hope this helps
Nice, this is also a cool way to achieve this, and I think a bit less complicated than the co routine approach I mentioned for a beginner to follow.
Answer by nutraj · Aug 03, 2020 at 12:00 PM
You should just use a co-routine and you will be good to go. In simple words, co-routines allow you to "control" the timing after which a piece of code will get executed. You can, and should, read up more about them : https://docs.unity3d.com/Manual/Coroutines.html
So, your code should look like this :
void SpawnerEnemy(float v)
{
float ExtendsX = EnemyPrefab.GetComponent<Renderer>().bounds.extents.x;
float ExtendsY = EnemyPrefab.GetComponent<Renderer>().bounds.extents.y;
float RandomX = Random.Range(Min.x + ExtendsX, Max.x - ExtendsX);
Vector2 RandomPosition = new Vector2(RandomX, Max.y + ExtendsY);
GameObject Enemy = Instantiate(EnemyPrefab, RandomPosition, Quaternion.identity);
Enemy.GetComponent<EnemyController>().Speed = Random.Range(MinSpeed, MaxSpeed);
StartCoroutine(StopSpawning());
}
private IEnumerator StopSpawning()
{
yield return new WaitForSeconds(180f);
CancelInvoke("SpawnerEnemy");
}
Also, I noticed you were checking whether the instantiated enemy prefab was inactive before cancelling the invoked repeating function, which is not needed if all you needed was to stop instantiating after 3 mins. Let me know further if you wanted to achieve something else.
Your answer
Follow this Question
Related Questions
How to Make an Expanding Spawn Area for Prefabs? 2 Answers
Add an offset distance between 2 spawned objects 1 Answer
Spawner won't work if I click too fast 0 Answers
Players Not Showing 0 Answers
Having Trouble with Instantiating an object on an axis 2 Answers