# error not all code paths return a value unity bool c#

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;
}
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 ! :)

"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.

• 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 – PandaPlay123 Oct 5 '18 at 12:20
• @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. – Jesse Williams Oct 5 '18 at 17:25
• thanks, i'll try that also, so that I can practice more different ways of coding :) – PandaPlay123 Oct 5 '18 at 17:36
• @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. ;) – DMGregory Oct 5 '18 at 21:52

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