-3

I am getting an error message That "not all code paths return a value" for this code.

 public bool IsInteractable(Vector3Int position)
    {
        //getting the tile
        TileBase tile = InteractableGround.GetTile(position);

        //Checking the name of the tilemap
        if (tile != null)
        {

            if (tile.name == "Interactable")
            {

                return true;

            }

            return false;

        }

    }

I think the issue might be with the string for the file name, but i have copy and paste the name of the tile.

derHugo
  • 83,094
  • 9
  • 75
  • 115

2 Answers2

1

It’s because when your if statement condition is not met your if block is skipped right? So the code inside your if statement is not executed.

You need to include a return false or similar after your if statement or in an else statement :)

ZikosFF
  • 44
  • 4
0

This error means that there is one possibility where your code won't have a return statement. Here, if your tile is null, you don't return anything, which is causing C# to throw this error.

edd
  • 49
  • 5
  • The error is from the c# compiler, not Unity – Rufus L Feb 19 '23 at 22:40
  • Yes, my bad it's "which is causing C# to throw this error". Basically comes down to the same result, you need to put a return statement at the end of your function. – edd Feb 19 '23 at 22:49
  • 1
    True! [Editing](https://stackoverflow.com/posts/75503232/edit) the answer would improve it's quality. – Rufus L Feb 19 '23 at 22:56