GameObject不返回正确的布尔值

时间:2014-11-20 12:49:10

标签: c# unity3d

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

在开始之前,我使用方法createTile()生成一个100x100的gameObject网格,用于" Walls"我将isWall设置为true,将#34}设置为免费"我将isWall设置为false。 (见方法)

然后在generateAisle方法中,我想创建一个走向North的过道(强制方向= 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);
    }
}

1 个答案:

答案 0 :(得分:0)

在建议@ LearnCocos2D后,我删除了所有&#34; GameObject.Find()&#34;并简单地返回在createTile()中创建的tile,并将引用存储在一个数组中。然后在需要时我检查存储的GameObject是否是墙。

相关问题