2

I was trying to make a c# script in unity, while the script says 'foodManager.foodEat(GameObject)': not all code paths return a value in line 61 (Marked with ** in the start and end of the line in the end of the script) about the IEnumerator. What's the problem here and how do I solve it?

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class FoodManager : MonoBehaviour
{

    public bool[] isFull;
    public GameObject[] slots;

    public GameObject[] itemNames;
    public GameObject[] itemImages;
    public GameObject[] itemAmounts;
    public GameObject foodObject;

    public string[] foodNames;
    public Sprite[] foodImages;
    public Sprite[] foodHalfImages;

    public GameObject foodPanel;
    void Update()
    {
        for(int i = 0; i < 6; i++)
        {
            if (isFull[i] == true)
                slots[i].SetActive(true);
            if (isFull[i] == false)
                slots[i].SetActive(false);
        }
    }

    private void addItem(int max, string itemName, GameObject itemImage, int addingAmount, string foodName)
    {
        for (int j = 0; j < max; j++)
        {
            if (isFull[j] == true && itemNames[j].GetComponent<Text>().text == itemName)
                itemAmounts[j].GetComponent<Text>().text = (int.Parse(itemAmounts[j].GetComponent<Text>().text) + addingAmount).ToString();
            if (isFull[j] == false)
            {
                isFull[j] = true;
                itemNames[j].GetComponent<Text>().text = itemName;
                itemAmounts[j].GetComponent<Text>().text = addingAmount.ToString();
                itemImages[j].GetComponent<Image>().sprite = foodImages[j];
                break;
            }
        }
    }

    public void foodButtonsBehavior(int a)
    {
        if(a >= 0 && a < 6)
        {
            StartCoroutine(foodEat(slots[a]));
        }
        if(a == 6) //exit Button
            foodPanel.SetActive(false);
    }

    **public IEnumerator foodEat(GameObject slot)**
    {
        foodPanel.SetActive(false);
    }
}

I want to use IEnumenator because I want to add WaitForSeconds() to the script, but I didn't add yet. Please help me solve this, it's very important!

Quartz
  • 37
  • 4
  • You have a function declared as `public IEnumerator foodEat(GameObject slot)`, but it doesn't have a `return` statement (let alone one that returns an `IEnumerator`). If you want it to return a collection, return one. If you don't want it to return anything, declare the function as a `void` function – Flydog57 Dec 13 '19 at 17:29
  • I searched Google for ["not all code paths return a value"](https://www.google.com/search?q=not+all+code+paths+return+a+value) and found [this](https://stackoverflow.com/q/21197410/1202807) and a ton of other answers. – Gabriel Luci Dec 13 '19 at 17:30

4 Answers4

2

The problem is that the method has a return type of IEnumerator, but does not return anything. It will not compile until you finish the foodEat method so that it returns an IEnumerator.

jasonmchoe
  • 491
  • 2
  • 6
2

In IEnumerator functions you should have at least one yield return smth, for your case it's yield return new WaitForSeconds(secondsToWait).

2

You didn't include a return statement. Your function signature mentions it'll return an ienumerator, yet in your 1 line of code you only set a value, returning nothing. Either write a return statement or use the void keyword!

LlamaSage
  • 73
  • 6
1

Set active returns nothing as you can see https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html. So change your code to

public void foodEat(GameObject slot)
{
    foodPanel.SetActive(false);
}

This should solve your problem.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61