I think my foreach loop is running multiple times and I can’t seem to fix it. Something is happening so that my Waves.number which is a static int as you can see is doubling (or something) every time v in Wave, goes to a new v.
using UnityEngine;
using System.Collections;
public class CreateMonteCarloShips : MonoBehaviour {
float timebetweenenemies = 1;
float Wavenumber;
public Waves[] Wave;
float timer = 5;
public delegate void roundnumber(int n);
public static roundnumber start;
public bool isrunning;
[System.Serializable]
public class Waves {
public string h;
public GameObject waveprefab;
public int enemiesinwave;
public int amountofships;
[System.NonSerialized]
public int enemiesspawned = 0;
public float timebetweeneachenemybatch;
[System.NonSerialized]
public static int waveNumber = 1;
}
void Start() {
start(1);
}
void Update () {
Debug.Log(timebetweenenemies);
if (timebetweenenemies <= 0 && isrunning == false)
{
isrunning = true;
Debug.Log("outsidefuncntion");
foreach (Waves v in Wave)
{
MonteCarlo[] enemieskilled = FindObjectsOfType<MonteCarlo>();
Debug.Log(v.h);
Debug.Log("beforeprintfunction");
printfunc();
if (v.enemiesspawned < v.enemiesinwave)
{
v.waveprefab.GetComponent<MonteCarlo>().amofships = v.amountofships;
Instantiate(v.waveprefab, v.waveprefab.transform.position, v.waveprefab.transform.rotation);
v.enemiesspawned++;
Debug.Log(v.enemiesspawned + "spawningenemy");
timebetweenenemies = v.timebetweeneachenemybatch;
break;
}
Debug.Log("Afterspawningenemy");
Debug.Log(enemieskilled.Length + "Lengthofmontecarloarray");
if (enemieskilled.Length != 0)
{
foreach (MonteCarlo m in enemieskilled)
{
if (m.isdead == false)
{
goto BREAK;
}
}
}
//timer -= Time.deltaTime;
//Debug.Log(timer);
//if (timer > 0)
//{
// break;
//}
//Debug.Log("log");
//timer = 5;
Waves.waveNumber++; //is multiplying. When it should be 1, it's 1.
start(Waves.waveNumber); //When it should be 2 it's 2.
//When it should be 3, it's 4.
//When it should be 4 it's 7.
//When it should be 5, it's 11.
Debug.Log(Waves.waveNumber + "Wavenumber");
}
BREAK:
isrunning = false;
}
timebetweenenemies -= Time.deltaTime;
}
public void printfunc() {
Debug.Log("printfunc");
}
}
