脚本上的布尔值,表示函数 C# Unity 的错误为“并非所有代码路径都返回一个值”

问题描述 投票:0回答:2

我收到此代码的“并非所有代码路径都返回值”的错误消息。

 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;

        }

    }

我认为问题可能出在文件名的字符串上,但我已经复制并粘贴了图块的名称。

c# unity3d unityscript
2个回答
0
投票

此错误意味着您的代码有一种可能没有返回语句。 在这里,如果您的图块为空,则不会返回任何内容,这会导致 Unity 抛出此错误。


0
投票

是因为当你的 if 语句条件不满足时,你的 if 块被跳过了对吗?所以你的 if 语句中的代码没有被执行。

您需要在 if 语句之后或 else 语句中包含 return false 或类似内容 :)

© www.soinside.com 2019 - 2024. All rights reserved.