实例化对象与控制台的输出不同。 (C#UNITY)

时间:2018-05-27 18:34:44

标签: c# android unity3d unityscript

enter image description here

正如您在控制台上方图片中看到的那样,前三个输出为BLUE,但我实例化的预制件中的前五个输出是不同的

enter image description here

所有BLUE预制件。

到目前为止,这是我的代码:

for(int col = 0; col < table.GetLength(0); col++){

        GameObject p = Instantiate(prefab_bigeye_gameobject) as GameObject;
        p.transform.SetParent (pos_big_eye_road);
        p.transform.localScale = Vector3.one;

        img = (RawImage)p.GetComponent<RawImage> ();
        for(int row = 0; row < table.GetLength(1); row++){
            if(table[moveCol, moveRow] == null){
                col0 = table[colIndex1, row];
                col1 = table[colIndex2, row];

                if(col0 != null)
                {
                    sum1+=1;
                }

                if(col1 != null)
                {
                    sum2+=1;
                }
                counter++;
            }
            else
            {
                if(counter == 0){
                    data1 = table[firstTableCol,firstTableRow];
                    data2 = table[secondTableCol,secondTableRow];
                    data3 = table[thirdTableCol,thirdTableRow];
                    data4 = table[fourthTableCol,fourthTableRow];
                }
                else{

                    if(moveRow > 1){
                        firstTableRow++;
                        secondTableRow++;
                        thirdTableRow++;
                        fourthTableRow++;
                    }else{
                        firstTableCol++;
                        secondTableCol++;
                        thirdTableCol++;
                        fourthTableCol++;
                    }
                    data1 = table[firstTableCol,firstTableRow];
                    data2 = table[secondTableCol,secondTableRow];
                    data3 = table[thirdTableCol,thirdTableRow];
                    data4 = table[fourthTableCol,fourthTableRow];

                }
                if(data1 != null && data2 != null && data3 != null && data4 != null)
                {

                    img.texture = NewTexture[1];
                    p.SetActive(true);
                    redcounter++;
                    print("RED ARE NOW AT:" + redcounter);
                }
                else
                {
                    img.texture = NewTexture[0];
                    p.SetActive(true);
                    bluecounter++;
                    print("BLUE ARE NOW AT:" + bluecounter);
                }

                if(moveRow + 1 < table.GetLength(1)){
                    moveRow++;
                }
            }
        }

        if(sum1 == sum2)
        {
            img.texture = NewTexture[1];
            p.SetActive(true);
            redcounter++;
            print("RED ARE NOW AT:" + redcounter);
        }
        else
        {
            img.texture = NewTexture[0];
            p.SetActive(true);
            bluecounter++;
            print("BLUE ARE NOW AT:" + bluecounter);
        }
        //reset
        sum1 = 0;
        sum2 = 0;

        if(colIndex1 + 1 < table.GetLength(0) && colIndex2 + 1 < table.GetLength(0))
        {
            colIndex1++;
            colIndex2++;
        }
        else
        {
            //nothing to do
        }

        if(moveCol + 1 < table.GetLength(0))
        {
            moveCol+=1;
        }
        if(moveRow + 1 < table.GetLength(1)){
            moveRow = 1;
        }
    }

预期输出必须是

BLUE , BLUE , BLUE, RED, BLUE

但是在我的输出中它是

BLUE, BLUE , BLUE, BLUE, BLUE

1 个答案:

答案 0 :(得分:0)

我不知道为什么直接在for循环中实例化它会让它变得疯狂所以我所做的就是通过创建函数来区分它们

void blueBead(){
    p = Instantiate(prefab_bigeye_gameobject) as GameObject;
    p.transform.SetParent (pos_big_eye_road);
    p.transform.localScale = Vector3.one;

    img2 = (RawImage)p.GetComponent<RawImage>();

    img2.texture = NewTexture[0];
    p.SetActive(true);
}

void redBead(){
    p = Instantiate(prefab_bigeye_gameobject) as GameObject;
    p.transform.SetParent (pos_big_eye_road);
    p.transform.localScale = Vector3.one;

    img2 = (RawImage)p.GetComponent<RawImage>();

    img2.texture = NewTexture[1];
    p.SetActive(true);
}

并像这样打电话给他们

if(data1 != null && data2 != null && data3 != null && data4 != null)
{

    readBead();
}
else
{
    blueBead();
}