How to wait for input in an update function (beginner issue)

Hi all,
here’s my situation: I’m working on a simple adventure/puzzle game concept. I’m trying to create a mechanic where a user can use an item from their inventory on an item in the game world (let’s call that action ‘combine’). Like this: upon finding an interactive object in the world (that has its own Combine element), the player presses the combine button (_combineAction) that would allow them to select an item from the inventory (using arrow buttons for example), upon which they would confirm the choice of item (with _selectItemAction) and then the game would pass the selected inventory item to combine function from the particular interactive object.

My issue is this: after calling the player’s combine action (currently done from within the player’s update function) the game doesn’t recognize the subsequent inputs… like selecting the items and confirming. I’ve tried different variations of loops, and also a coroutine but no luck so far. I understand that this is probably caused by the fact that update is called every frame but how to implement something like this, then? I’m very new to game programming (though not to programming as such) so not even looking for a code fix, but rather what are the concepts I need to look into? More coroutines? Unity events? Something else completely?

My code looks sort of like this (only including the relevant parts):

private void Update()
    {


                // Combine
                if (_combineAction.WasPressedThisFrame() && _interactableCombine != null)
                {
                    // Start at the first inv position
                    int itemIndex = 0;

                    // Here the player would chose an item from the inventory - not implemented yet (always using the first item, index=0)

                    // Once you choose an item, press a button to interact
                    Debug.Log($"Press {GetActionLabel(_selectItemAction)} to use item");

                   // This is the coroutine that should wait for the selectItemAction
                   // _interactableCombine is the combine element of the game world object I'm trying to interact with
                    waitForItemSelect(itemIndex, _interactableCombine);
                }

    }

And this is the coroutine:

    private IEnumerator waitForItemSelect(int itemIndex, Combine interactableCombine)
    {
        while (!_selectItemAction.WasPressedThisFrame())
        {
            yield return null;
        }
        var selectedItem = GetInvSlotItem(itemIndex);
        // On success we should stop the selection process
        if (interactableCombine.Combine(selectedItem)) Debug.Log("Success.");
        // On failure we shouldn't stop but let player know it was a failure
        else Debug.Log("Wrong item.");

    }

When I run this I get the debug message prompting me to press the select key but when I do nothing happens, the coroutine is completely ignored.

Many thanks in advance.

  1. StartCoroutine(waitForItemSelect(itemIndex, _interactableCombine));

Oarsman, if you wanna get across the River Styx, you gotta START this coroutine!

As Zulo notes, you gotta call StartCoroutine().

Coroutines in a nutshell:

https://discussions.unity.com/t/825667/6

https://discussions.unity.com/t/749264/9

Splitting up larger tasks in coroutines:

https://discussions.unity.com/t/840656/2

Coroutines are NOT always an appropriate solution: know when to use them!

“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums

https://discussions.unity.com/t/857304/4

https://discussions.unity.com/t/849607/7

Our very own Bunny83 has also provided a Coroutine Crash Course:

https://answers.unity.com/questions/1749615/coroutines-ienumerator-not-working-as-expected.html?childToView=1749714#answer-1749714

Awesome, guys, thanks! Feeling rightfully stupid right now.
@Kurt-Dekker many thanks for all the resources, I’ll be sure to go through them!

No need at all to feel stupid!

After starting ten million functions with a simple foo(); calling syntax, even today I still occasionally forget to wrap that with StartCouroutine(foo()); when it is a coroutine. Muscle memory…

The key is to find it fast… be on the lookout, etc.

Coroutines are really messy. I still often have to look at the manual to use them. I know Invoke is frowned upon but god is it easy to use… :slight_smile: