unity gameobject材质错误

时间:2019-06-08 22:05:50

标签: c# unity3d

我有一个创建红色和黄色块的程序。我想要一个数组,其中所有块都必须为红色(代码中的userRow)。 BTW colors[0] =红色,colors[1] =黄色。问题是,当我将数组中的所有对象都设置为colors[0](红色)时,仍然有一些黄色的块。

奇怪的是,我放了一个debug.log,它没有意识到任何块都是黄色的,即使它们是黄色的。我将向您简要介绍黄色的块和输出。

image with yellow blocks

private void Generate() 
{ 
    //set the number of colored blocks numToColor = level 2 + 3; 
    // Create empty grid row = new GameObject[zNum xNum yNum]; userRow = new GameObject[zNum xNum yNum]; alltrue = new bool[yNum xNum * zNum];

     //  level
     //     numToColor
     // Create blocks
     for (int i = 0; i < yNum; i++)
     {
         for (int j = 0; j < xNum; j++)
         {
             for (int k = 0; k < zNum; k++)
             {
                 //display table
                 GameObject g = Instantiate(displayBlock, new Vector3(j * offset, i * offset, k * offset), Quaternion.Euler(180, 0, 0), displayBlockParent.transform);
                 row[i * (xNum * zNum) + j * zNum + k] = g;
                 //coloring table
                 GameObject o = Instantiate(userBlock, new Vector3(j * offset, i * offset, k * offset), Quaternion.Euler(180, 0, 0), userBlockParent.transform);
                 userRow[i * (xNum * zNum) + j * zNum + k] = o;
                 userRow[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color = colors[0];
             }
         }
     }
     //reverse the array because it goes from last to first
     Array.Reverse(row);
     Array.Reverse(userRow);
     //create pattern
     for (int s = 1; s < numToColor; s++)
     {
         List<GameObject> colored = new List<GameObject>();
         List<GameObject> notColored = new List<GameObject>();
         if (row.ToList<GameObject>().All(p => p.GetComponent<Renderer>().material.color == colors[0]))
         {
             int randomIndex = UnityEngine.Random.Range(0, yNum * xNum * zNum);
             row[randomIndex].GetComponent<Renderer>().material.color = colors[1];
         }
         for (int i = 0; i < yNum; i++)
         {
             for (int j = 0; j < xNum; j++)
             {
                 for (int k = 0; k < zNum; k++)
                 {
                     if (row[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color == colors[1])
                     {
                         colored.Add(row[i * (xNum * zNum) + j * zNum + k]);
                     }
                 }
             }
         }
         for (int i = 0; i < yNum; i++)
         {
             for (int j = 0; j < xNum; j++)
             {
                 for (int k = 0; k < zNum; k++)
                 {
                     if (row[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color == colors[0] && (colored.Any(p => (j != xNum - 1 && GameObject.ReferenceEquals(p, row[i * (xNum * zNum) + (j + 1) * zNum + k]))
                           || (j != 0 && GameObject.ReferenceEquals(p, row[i * (xNum * zNum) + (j - 1) * zNum + k]))
                           || (i != 0 && GameObject.ReferenceEquals(p, row[(i - 1) * (xNum * zNum) + j * zNum + k]))
                           || (i != yNum - 1 && GameObject.ReferenceEquals(p, row[(i + 1) * (xNum * zNum) + j * zNum + k]))
                           || (k != 0 && GameObject.ReferenceEquals(p, row[i * (xNum * zNum) + j * zNum + k - 1]))
                           || (k != zNum - 1 && GameObject.ReferenceEquals(p, row[i * (xNum * zNum) + j * zNum + k + 1])))))
                      {
                         notColored.Add(row[i * (xNum * zNum) + j * zNum + k]);
                     }
                 }
             }
         }
         notColored.ToArray()[UnityEngine.Random.Range(0, notColored.ToArray().Length)].GetComponent<Renderer>().material.color = colors[1];
     }
     for (int i = 0; i < yNum; i++)
     {
         for (int j = 0; j < xNum; j++)
         {
             for (int k = 0; k < zNum; k++)
             {
                 if(userRow[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color == colors[0])
                 {
                 }
                 else if(userRow[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color == colors[1])
                 {
                     Debug.Log("x: " + j + " y: " + i + " z: " + k + " || color:yellow error");
                 }
                 else
                 {
                     Debug.Log("x: " + j + " y: " + i + " z: " + k + " || color: error");
                 }
             }
         }
     }
 }

现在您可以看到我有许多debug.logs。它记录了其中的任何一个。它应该已经记录了“错误黄色”,但是没有。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我的猜测是还有另一个脚本可以更改这些多维数据集的颜色。您是否检查过是否正在其他脚本中访问它们?