Home > Software design >  How to activate and deactivate GameObjects with a delay in Untiy
How to activate and deactivate GameObjects with a delay in Untiy

Time:02-13

I am working on a game, that has a keypad puzzle. A specific key combination lights up one by one, which the player must repeat to solve that puzzle. I am going to let the player know what the combination is by activating and deactivating some GameObjects systematically, one by one. As it suggests, there is some time delay between the deactivation of one GameObject and the activation of another. The problem is, in my code, all the GameObjects activate simultaneously instead of one by one, after a delay.

Here is the code:

 public string Generate(int length, float delay)
    {
        // Variables for logic
        string combination = "";
        int prev = -1; int current = 0;
        int rnd = 0;

        for (int i = 0; i < length; i  )
        {
            rnd = Random.Range(0, BUTTONS);
            while (rnd == prev)
            {
                rnd = Random.Range(0, BUTTONS);
            }
            prev = current;
            current = rnd;

            combination  = current.ToString();

            // Activation and Deactivation

            StartCoroutine(GenerateDelay(delay, current));
        }
        return combination;
    }

    IEnumerator GenerateDelay(float delay, int index)
    {
        ButtonClicks[index].SetActive(true);
        yield return new WaitForSeconds(delay);
        ButtonClicks[index].SetActive(false);
    }

The loop counter specifies the length of the combination. I believe there is something wrong with the Coroutine I made? Since all the objects activate simultaneously.

Here is the result in the game as well:

Keypad

We can see, only one button should turn green at a time, but all of them do in this case. Any solutions?

CodePudding user response:

You start all your Coroutines parallel so things happen at the same time.

StartCoroutine does not delay the method which calls it (unless it is yielded as well.

You would need to run the entire loop within a Coroutine in order to delay it as well.

You could simply split up the creation of the combination and while you already return it you start the visualization in the background in parallel

public string Generate(int length, float delay, Action<string> onCombination)
{
    // Variables for logic
    var combination = List<int>();
    var prev = -1; 

    for (var i = 0; i < length; i  )
    {
        int rnd;
        do
        {
            rnd = Random.Range(0, BUTTONS);
        }
        while (rnd == prev);

        prev = rnd;

        combination.Add(rnd);
    }

    StartCorouine (ShowCombination(combination, delay));

    return string.Join("", combination);
}

private IEnumerator ShowCombination (IEnumerable<int> combination, float delay)
{
    foreach(var i in combination)
    {
        ButtonClicks[i].SetActive(true);
        yield return new WaitForSeconds(delay);
        ButtonClicks[i].SetActive(false);
    }
}

CodePudding user response:

something alot easier then a coroutine is invoking a function, basically create a function to activate/deactivate the wanted object and whenever you want to call it do:

Invoke("FUNCTIONNAME", TIME);

and it will run the function specified after the TIME.

  • Related