Make a gameobject spawn before enemies spawn? (C#)
Hello! I'm developing a 2D mobile game and I have a little problem. I have created a spawn system, that spawns bouncing balls at a random location inside a rectangle. I'm now trying to create a spawn system for another gameobject, this gameobject is meant to show where the bouncing ball will spawn. It'll only show up for a second just so the player can identify and highlight that "Ahhh an enemy ball will spawn here". The Gameobject is called "ShowEnemySpawn" and I instantiate it in the same function that spawns out the enemy balls using the same position, I also use InvokeRepeating to make the bouncing balls spawn out every 2 seconds.
How do I spawn the "ShowEnemySpawn" Gameobject for 1 sec before the Bouncing enemy ball spawn?
I've been trying to search for a solution, but has been unable to find a one. So I'm really grateful for any help I can get! Here is the code!
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
// EnemyPrefab
public GameObject EnemyBouncingPrefab;
//Show where spawn is prefab
public GameObject ShowEnemySpawn;
// Borders
public Transform borderTop;
public Transform borderBottom;
public Transform borderLeft;
public Transform borderRight;
void Start ()
{
// Spawn boucning enemies every 2 seconds, starting in 3
InvokeRepeating("SpawnBouncingEnemy", 3, 2);
}
// Spawn a Bouncing enemy
void SpawnBouncingEnemy()
{
// x position between left & right border
int x = (int)Random.Range (borderLeft.position.x,
borderRight.position.x);
// y position between top & bottom border
int y = (int)Random.Range (borderBottom.position.y,
borderTop.position.y);
// Sets the prefab where an enemy will spawn
Instantiate (ShowEnemySpawn, new Vector2 (x, y), Quaternion.identity);
// Instantiate the power up at (x, y)
Instantiate (EnemyBouncingPrefab,
new Vector2 (x, y),
Quaternion.identity); // default rotation
}
}
Answer by Statement · Mar 06, 2016 at 04:38 PM
You can use coroutines to easily write code that can suspend execution a second midway a function.
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
// EnemyPrefab
public GameObject EnemyBouncingPrefab;
//Show where spawn is prefab
public GameObject ShowEnemySpawn;
// Borders
public Transform borderTop;
public Transform borderBottom;
public Transform borderLeft;
public Transform borderRight;
void Start()
{
StartCoroutine(SpawnBouncingEnemy());
}
// Spawn boucning enemies every 2 seconds, starting in 3
IEnumerator SpawnBouncingEnemy()
{
yield return new WaitForSeconds(3);
while (true)
{
Vector2 spawnPoint = RandomPointWithinBorders();
// Show spawn location for one second
Object marker = Instantiate(ShowEnemySpawn, spawnPoint, Quaternion.identity);
yield return new WaitForSeconds(1);
Destroy(marker);
// Spawn enemy
Instantiate(EnemyBouncingPrefab, spawnPoint, Quaternion.identity);
yield return new WaitForSeconds(1);
}
}
Vector2 RandomPointWithinBorders()
{
Vector2 random = new Vector2();
random.x = (int)Random.Range(borderLeft.position.x, borderRight.position.x);
random.y = (int)Random.Range(borderBottom.position.y, borderTop.position.y);
return random;
}
}
Genius! It works! I'll definitely check out coroutines more in the future. Thank you so much for your help Statement!
Your answer
Follow this Question
Related Questions
How to spawn multiple prefabs at different spawn rates 0 Answers
Spawn system that doesn't instantiated enemies on top of player or each other (C#) 2 Answers
Check if there's no GameObject with specific tag in the field (in the camera view) 0 Answers
Improving script which Spawns enemies up to a required number 0 Answers
How do I spawn a certain amount of objects randomly between 2 positions? 0 Answers