我收到此代码的“并非所有代码路径都返回值”的错误消息。
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;
}
}
我认为问题可能出在文件名的字符串上,但我已经复制并粘贴了图块的名称。
此错误意味着您的代码有一种可能没有返回语句。 在这里,如果您的图块为空,则不会返回任何内容,这会导致 Unity 抛出此错误。
是因为当你的 if 语句条件不满足时,你的 if 块被跳过了对吗?所以你的 if 语句中的代码没有被执行。
您需要在 if 语句之后或 else 语句中包含 return false 或类似内容 :)