- Home /
Gameobject not the right color
hi, I have a program where ceratin blocks have to be yellow and some red. I have an array were all the blocks have to be red (userRow in the code). BTW colors[0] = red, colors[1] = yellow. hwne I set all the object in my array to color[0] htere are still some blocks that are yellow. The weired thing is that I put a debug.log and it didnt recognize that any of the blocks were yellow even though they were. I will give you a snap of the blocks that are yellow and the output.
And the code snippet
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");
}
}
}
}
}
there is no output in the console, and as you can see 2 of the blocks are obviously yellow. I even went to the inspector and it says the material is colored yellow. Can somebody help me?