Problem with my Coroutine
Hi i am creating an asteroid game and for my asteroids i used a Coroutine. Now i am trying to create boss fight after i destroyed 100 asteroids. How can i do that. I tried to stop the loop but it wont turn back again. Also if i set up do to something when is bigger then 100 asteroids the other setups wont stop. Sorry for my English and also for my post i am new to this domain. Thank you very much.
public class RandomHazzards : MonoBehaviour {
public GameObject Hazzard;
public GameObject Hazzard2;
public GameObject Hazzard3;
public GameObject Hazzard4;
public GameObject Hazzard5;
public GameObject Hazzard6;
public GameObject Hazzard7;
public GameObject Spawner;
public Vector3 SpawnValues;
public Vector3 SpawnValues2;
public Vector3 SpawnValues3;
public Vector3 SpawnValues4;
public Vector3 SpawnValues5;
public Vector3 SpawnValues6;
public Vector3 SpawnValues7;
public int HazzardCount;
public float spawnwait;
public float StartWait;
public float WaveWait;
public float IncWave;
public float Waveplus = 0.2f;
public int DestroyedAsteroids;
void Start()
{
StartCoroutine (SpawnWaves ());
}
void Update()
{
GameObject theasteroid = GameObject.Find("GameController");
GameController gamecont = theasteroid.GetComponent<GameController>();
if (gamecont == null)
{
// Debug.Log("Game COntroller not found");
}
if (gamecont != null)
{
// Debug.Log("Itworks");
DestroyedAsteroids = gamecont.asteroidskilled;
}
IncWave = WaveWait - Waveplus *Time.deltaTime ;
if (IncWave <= 0.5f) {
IncWave = WaveWait;
}
}
IEnumerator SpawnWaves() {
yield return new WaitForSeconds(StartWait);
while (true) {
for (int i = 0; i < HazzardCount; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range (SpawnValues.x, -SpawnValues.x), SpawnValues.y, SpawnValues.z);
Vector3 spawnPosition2 = new Vector3 (Random.Range (SpawnValues2.x, -SpawnValues2.x), SpawnValues2.y, SpawnValues2.z);
Vector3 spawnPosition3 = new Vector3 (Random.Range (SpawnValues3.x, -SpawnValues3.x), SpawnValues3.y, SpawnValues3.z);
Vector3 spawnPosition4 = new Vector3 (Random.Range (SpawnValues4.x, -SpawnValues4.x), SpawnValues4.y, SpawnValues4.z);
Vector3 spawnPosition5 = new Vector3 (Random.Range (SpawnValues5.x, -SpawnValues5.x), SpawnValues5.y, SpawnValues5.z);
Vector3 spawnPosition6 = new Vector3 (Random.Range (-SpawnValues6.x, SpawnValues6.x), SpawnValues6.y, SpawnValues6.z);
Vector3 spawnPosition7 = new Vector3 (Random.Range (-SpawnValues7.x, SpawnValues7.x), SpawnValues7.y, SpawnValues7.z);
Quaternion SpawnRotation = Quaternion.identity;
if (DestroyedAsteroids >= 0) {
Instantiate (Hazzard, spawnPosition, SpawnRotation);
Instantiate (Hazzard2, spawnPosition2, SpawnRotation);
Instantiate (Hazzard3, spawnPosition3, SpawnRotation);
Instantiate (Hazzard4, spawnPosition4, SpawnRotation);
Instantiate (Hazzard5, spawnPosition5, SpawnRotation);
}
if (DestroyedAsteroids >= 20) {
Instantiate (Hazzard6, spawnPosition6, SpawnRotation);
}
if (DestroyedAsteroids >= 40) {
Instantiate (Hazzard7, spawnPosition7, SpawnRotation);
}
if (DestroyedAsteroids >= 70) {
Instantiate (Hazzard6, spawnPosition6, SpawnRotation);
Instantiate (Hazzard7, spawnPosition7, SpawnRotation);
}
if (DestroyedAsteroids >= 90) {
Instantiate (Hazzard7, spawnPosition7, SpawnRotation);
}
yield return new WaitForSeconds (IncWave);
}
}
}
}
Answer by Greg_lrgg · Mar 05, 2018 at 09:22 AM
Hey,
First off you might want to use Arrays to store your hazards and spawnpos, it 'll make your code clearer and more attractive to look at. Believe me this might be "secondary" to you but having a good looking code is better both for you and people who are trying to help you.
Secondly, if you have to type the same instructions more than a few times (namely 2 or 3) it means that something is either wrong or poorly optimised in your code. You can declare a function that spawns several of your hazards at several spwan position.
Now to answer your question you should not do the test inside of your coroutine, which is what you are doing if I read correctly. Do it in your main code, and create a dedicated instructions to spawn your boss.
Lastly to be able to start and restart a coroutine, you should do the following:
First of all declare a variable IEnumerator myCoroutine which will hold your actual coroutine. When you stop a coroutine, it actually does not "exist" anymore to make it simple, so you should have a line that says to Unity "Hey this is the coroutine I want to use" whenever you want to start it again.
Namely: myCoroutine = MyCoroutine(), where MyCoroutine is your implemented coroutine. This way Unity will have a correct reference your coroutine when you will say:
StartCoroutine( myCoroutine)
Your answer
Follow this Question
Related Questions
Can somebody tell me which functions I would need to use to make this pseudocode work? 1 Answer
[Unity] Cómo agrego la salida y la entrada en el laberinto Unity? 0 Answers
Unity IAP Disable Ads 0 Answers
adding two integers together once every half second,adding two integers together with delay? 1 Answer