foreach loop running multiple times I think.

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");
    }
}

Is there multiple CreateMonteCarloShips components attached to GameObjects out there?

Note, that static fields are globally static. This means that if you have more than 1 instance of a script, they’ll be modifying the same variable.

Also

This is quite literally gibberish to anyone not you. What is Waves.number? What is ‘v’? What is the act of v going to a new v?

Furthermore, your variable names are unwieldly and confusing.

float Wavenumber - ? what is this? number of waves? the current wave? What is a wave? Is this a wave in the water (I see a reference to ships), or a wave of enemies, or what is it?

Waves[ ] Wave - I love that your collection (many) is named a singular name, yet the type is plural… WAT!? Wouldn’t Wave[ ] Waves make more sense?

A delegate called ‘roundnumber’, and a variable of that type called ‘start’ (not to be confused with Start), which is never assigned in the scope of the code shown… what in holy hell is that even?

string h - single letter field/variable names are the least descriptive things in the world. The only time they really should be used is in short scoped code where it’s very obvious… like ‘i’ for an iterator in a for loop.

int enemiesinwave - see, I have NO ides what a Wave is still… this implies enemies… but ships? what!?

int amountofships - are ships enemies are waves groups are my brain getting lost

float timebetweeneachenemybatch - isthisdifficulttoreadbecauseithinkitis? howAboutCamelNotationOnTheOtherHand? Yeah… try it.

int waveNumber - as opposed to float Wavenumber… WAT!?

Waves.number is just going to be the wavenumber. What you are on. When the foreach goes to the next iteration it’s going to increase the wave, because then you are on a new wave. It’s going to show up on the screen only. I send it through the delegate to another script which will only show it on the screen.

The array Waves[ ] is storing all the waves. In wave 1 there will be on row of enemies, with 1 spaceship.

In wave 2 there will also be on row of enemies, with 1 spaceship on that row.

If I increase enemiesinwave there will be more rows of enemies, and each rowofenemy will have x amountofships. The rows of enemies will spawn every “timebetweeneachenemybatch”.

I don’t have any other instances of this script running I am pretty sure.

Here is a gyazo gif maybe explaining it a little bit better:
https://gyazo.com/e2e1722cef19da092803a3592219fd7b

In this gif I spawn 5 rows of enemies, and each row have 10 enemies in them. They spawn every 0.1 second.

https://gyazo.com/7d5b52c92e2fa3290cb7d30ebc4e0233

The Monte Carlo script itself is the one controlling one row of enemies. It’s moving one row of enemies.

the h was just for testing purposes, trying to debug, so that I could print out the string that was in h, and see which wave was running and figure out what was actually wrong, and the float WaveNumber I didn’t actually realize I made to a float, but I am not using it in my script anyway. It was also just for debugging purposes.