【问题标题】:GameObject doesn't return the correct boolean valueGameObject 没有返回正确的布尔值
【发布时间】:2014-11-22 06:09:38
【问题描述】:

我在 c# 中创建了一个名为 Tile 的类,具有布尔属性,当我尝试通过方法 getIsWall() 访问它时,我总是得到 True。附上代码。

在开始之前,我使用createTile() 方法生成一个 100x100 的游戏对象网格,对于“墙”,我将 isWall 设置为 true,对于“免费”图块,我将 isWall 设置为 false。 (见方法)

然后在generateAisle 方法中,我想创建一个向北的过道(强制方向 = 1 用于调试情况),并使用方法checkIfWall() 检查 3 个瓷砖是否是墙壁。当我这样做时,我总是返回TRUE,但我知道有时一个图块是FALSE(在游戏运行时检查)。

有人可以帮助我吗?谢谢!

public void generateAisle(int x, int z, int lengthAisle){
    bool free = false;
    bool free2 = false;
    bool free3 = false;
    int direction = 1;//Random.Range (1, 4);
        if (direction == 1) {//north
            int tempZ = z+1;
            for (int i=0; i<lengthAisle; i++) {
                free = checkIfWall (x, tempZ+i);
                free2 = checkIfWall ((x-1), tempZ+i);
                free3 = checkIfWall ((x+1), tempZ+i);
                if(!free || !free2 || !free3){
                    free = false;
                    break;
                }
            }
            if(free){
                for (int i=0; i<lengthAisle; i++) {
                    /*Destroy(GameObject.Find("Tile"+x+"."+(tempZ+i)));
                    createTile(x,z+i,"Free");*/
                }
            }
        } else if (direction == 2) {//south
            int tempZ = z-1;
            for (int i=0; i<lengthAisle; i++) {
                free = checkIfWall (x+i, tempZ);
                free2 = checkIfWall (x-1+i, tempZ);
                free3 = checkIfWall (x+1+i, tempZ);
                if(!free || !free2 || !free3){
                    free = false;
                    break;
                }
            }
            if(free){
                for (int i=0; i<lengthAisle; i++) {
                    Destroy(GameObject.Find("Tile"+x+"."+(tempZ+i)));
                    createTile(x,z+i,"Free");
                }
            }
        } else if (direction == 3) {//east
            for (int i=0; i<lengthAisle; i++) {
                free = checkIfWall (x, z + i);
            }
        } else if (direction == 4) {//west
            for (int i=0; i<lengthAisle; i++) {
                free = checkIfWall (x, (z - i));
            }
        }
}
private bool checkIfWall(int x, int z){
    Debug.Log (GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile> ().getIsWall());
    if (GameObject.Find ("Tile" + x + "." + z) != null) {
        Debug.Log ("Tile" + x + "." + z + " = " + GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile> ().getIsWall());
        if (GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile> ().getIsWall()) {
            return true;
        } else {
            return false;
        }
    }else {
        return false;
    }
}

public void createTile(int x, int z, string kind="Wall"){
    GameObject newTile = (GameObject)Instantiate(Resources.Load(kind, typeof(GameObject)));
    newTile.name = "Tile"+x+"."+z;
    newTile.gameObject.GetComponent<Tile>().setPosition(x,z);
    if (kind == "Wall") {
        newTile.transform.position = new Vector3 (x, 1, z);
        newTile.GetComponent<Tile>().setWall(true);
    }else{
        newTile.transform.position = new Vector3 (x, 0, z);
        newTile.GetComponent<Tile>().setWall(false);
    }
}

【问题讨论】:

  • 发布 getIsWall 函数。请注意,频繁执行查找和频繁使用 + 附加字符串是非常低效的。您应该“找到”一次对象并将其存储在局部变量中,而不是构造字符串并一遍又一遍地找到它。您也可以只执行“return ... getIsWall()”而不是 if/else 返回 true/false,具体取决于 getIsWall 是否返回 true/false。
  • 你是对的hte方法checkIfWall,不是一个好的解决方案,我会尽量简化。那我就听从你的建议,我会在创建时将游戏对象的引用保存在一个数组中!

标签: c# unity3d


【解决方案1】:

在@LearnCocos2D 的建议下,我删除了所有“GameObject.Find()”,并简单地返回了在 createTile() 中创建的图块并将引用存储在一个数组中。然后在需要时检查存储的游戏对象是否为墙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 2018-08-11
    • 2013-09-15
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多