Code uncertainties

If have done the coding chalange like that, was it a problem?
The code:

 void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
        ProcessInputSpace();
        ProcessInputA();
        ProcessInputD();
    }

    private void ProcessInputSpace()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            print("Space pressed");
        }
        
    }
    private void ProcessInputA()
    {
        if (Input.GetKey(KeyCode.A))
        {
            print("A pressed");
        }

    }
    private void ProcessInputD()
    {
        if (Input.GetKey(KeyCode.D))
        {
            print("D pressed");
        }

    }

Hi ivank000,

The problem you got there is pretty simple.
By making separate functions for each key Binding you did not respect the challenge statement and both A and B will return messages.
Why is that?
Because the update() function is called at every frame so every function call inside the update() will be called in the order you have there. In your current development you have no way of stopping the call of the function ProcessInputD() if the ProcessInputA() is called.
When the ProcessInputA() function has done its job the update() will still have one more function to be called and will call ProcessInputB().

1 Like

Privacy & Terms