How to fix Not all code paths return a value error

I’m getting an error, supposedly because my Boolean method doesn’t return anything, even though added a return statement into it.
Here’s the code causing this error:

public static bool mouseClicked(MouseButton button)
        {
            if (button == MouseButton.Left)
            {
                return Input.GetMouseButtonDown(0);
            }

            if (button == MouseButton.Middle)
            {
                return Input.GetMouseButtonDown(2);
            }

            if (button == MouseButton.Right)
            {
                return Input.GetMouseButtonDown(1);
            }
        }

Any answers would be greatly appreciated!

You didn’t cover all code-paths.

You always have to return a true/false. What do you think happens if it gets to line 17? You don’t specify returning true/false.

The compiler isn’t told to check all possible values of “MouseButton”. In the end, you have to choose to assert or return true/false. Alternately use a switch statement but even then, you have to specify a “default”.

You can assume the final one is “MouseButton.Right” and just return “return Input.GetMouseButtonDown(1)” instead if you want.

Also, technically MouseButton is an enum like this:

public enum MouseButton
    {
        LeftMouse = 0,
        RightMouse = 1,
        MiddleMouse = 2
    }

so you could just pass it into the GetMouseButtonDown as an int like this:

    public static bool mouseClicked(MouseButton button)
    {
        return Input.GetMouseButtonDown((int)button);
    }
2 Likes

Oh thanks!

2 Likes