简体   繁体   中英

I have a for-loop in C# I want to run only once

I have a for-loop that I need to call only once as I'm iterating through an array.

    public void ChangeClassButton()
    {
        Debug.Log("Changed Class");
        for (int i = 0; i < activeDrum.Length && !changeClassBool; i++)
        {
            Debug.Log(activeDrum[i]);
            changeClassText.text = activeDrum[i];
            OscMessage oscM = Osc.StringToOscMessage("/changeClass" + i);
            Debug.Log(Osc.OscMessageToString(oscM));
            oscHandler.Send(oscM);
            changeClassBool = true;
            if (i >= activeDrum.Length)
            {
                i = 0;
            }
        }
    }

I have a seperate function setting the changeClassBool back to false after a set amount of time.

I'm trying to get an iteration on a button press, but when I press the button in the game it just iterates through the whole array. Any suggestions to as what I'm doing wrong?

A for loop might not be the best way to go about this. How about just upping the value when you call the method?

private int count = 0;

public void ChangeClassButton()
{
    Debug.Log("Skiftede klasse");
    if(!changeClassBool){
    {
        Debug.Log(activeDrum[count]);
        changeClassText.text = activeDrum[count];
        OscMessage oscM = Osc.StringToOscMessage("/changeClass" + count);
        Debug.Log(Osc.OscMessageToString(oscM));
        oscHandler.Send(oscM);
        changeClassBool = true;
        if (count >= activeDrum.Length)
        {
            count = 0;
        } else{
            count++;
        }
    }
}

Also, as to why it's not working now. You mention that you set changeClassBool to false after a certain amount of time. But this loop is run instantly, meaning that it will complete in one frame, no matter how big the loop is. If you want to wait in your loop you can make it a coroutine and add a yield return null to make it wait one frame.

But if the only thing you want to do, is to go through this each time you click a button, just using a counter instead of a loop is way less complex and more stable.

use the while loop.

int i = 0;
        public void ChangeClassButton()
        {
            while (!changeClassBool)
            {
                Debug.Log(activeDrum[i]);
                changeClassText.text = activeDrum[i];
                OscMessage oscM = Osc.StringToOscMessage("/changeClass" + i);
                Debug.Log(Osc.OscMessageToString(oscM));
                oscHandler.Send(oscM);
                changeClassBool = true;
                if (i >= activeDrum.Length)
                {
                    i = 0;
                }
                else
                {
                    ++i;
                }
            }
        }

This loop runs once.

if all you need is to iterate through this array (activeDrum) only once why not use "for each"? it might also save you the extra boolean you have in this for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM