0
\$\begingroup\$

I'm new to unity, so I've been having problems with my code here:

public bool FindItem(GameObject item)
{
    for (int i = 0; i < MyInventory.Length; i++)
    {
        if (MyInventory[i] == item)
        {
            //we found the item
            return true;
        }
        //if item not found
        return false;
    }
}

it shows this error : Assets/Script/Items/InventoryScript.cs(35,17): error CS0161: `InventoryScript.FindItem(UnityEngine.GameObject)': not all code paths return a value

I've been looking for answers on the internet but nothing seems to give me solution to this problem. so I was hoping you could help me, thanks again in advance ! :)

\$\endgroup\$
3
\$\begingroup\$

"Not all code paths return a value" means that inside a function that's supposed to explicitly return something (ie. it's not void or a constructor/IEnumerator), the compiler found a way to hit the end of the function without a return statement telling it what it's supposed to return.

In this case, this happens if MyInventory.Length is zero. What happens:

  • We enter the for loop and set i = 0

  • We check the for loop's test, is i < MyInventory.Length? No, zero is not less than zero.

  • Since we failed the test, we exit the for loop immediately, without visiting any of the code inside (not the if MyInventory[i] == item, not the return true, not the return false

  • Since there's nothing after the for loop, we hit the end of the function, but the compiler doesn't know what bool value the function should return because we haven't encountered a return statement.

Thus, the error.

To fix this in this case, you want to move the return false statement outside your for loop to the end of the function. When you put it inside the loop, the loop will check whether the item is in MyInventory[0], and if not, it will always return false before it gets a chance to check MyInventory[1]. You want to return true if and only if we get through the whole array and don't find the item, not immediately after a single mismatch.

Or you can replace the whole thing with

public bool FindItem(GameObject item) {
    return System.Array.IndexOf(MyInventory, item) >= 0;
}

IndexOf returns -1 if the item isn't found in the array.

As Mikael says, this is a pure programming question that doesn't need game-specific expertise, so you can find answers to this elsewhere too.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank you so much! :D what you said is totally correct, I double check my code and my return false is inside the for loop which is incorrect. you saved me DMGregory :D \$\endgroup\$ – PandaPlay123 Oct 5 '18 at 12:20
  • \$\begingroup\$ @DMGregory is absolutely correct. However, if you ever find yourself with a more complex function returning a bool (or returning anything, really) you should always have a default return value (for me that is often false) at the end. Either by literally just having a return false; before your close bracket, or by setting a local bool variable that you set to a default at the top, then change as it hits any validations, and just return _bool; at the end. I tend to use both a lot. \$\endgroup\$ – Jesse Williams Oct 5 '18 at 17:25
  • \$\begingroup\$ thanks, i'll try that also, so that I can practice more different ways of coding :) \$\endgroup\$ – PandaPlay123 Oct 5 '18 at 17:36
  • 1
    \$\begingroup\$ @JesseWilliams myself, I'd recommend not adding a return statement where you don't expect one to be hit. That way, the compiler can alert you when your assumption is wrong. If you add a return statement "just in case," then when you later make a change that accidentally allows control flow to take a path you didn't expect, that new path gets accepted silently, without any reminder to double-check your assumptions. It might be more annoying that way, dealing with the occasional spurious error, but it helps keep you vigilant, and makes your intention/expectation explicit in the code. ;) \$\endgroup\$ – DMGregory Oct 5 '18 at 21:52
1
\$\begingroup\$

If MyInvenyory does not contain any items the for loop will never execute, therefore it will skip the entire block of code.

To fix you should add a return statement after your loop.

Usually these kinds of questions are not encouraged since it's not directly related to gamedev and are more of a "debug my code" question, but since you are a new contributor I figured I might answer it anyway.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ ok got it, thanks Mikael for all the help :) and I'll make sure that my questions next time will be related to gamedev :D \$\endgroup\$ – PandaPlay123 Oct 5 '18 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.