Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by itaielidan · Jun 09, 2019 at 10:12 PM · gameobjectmaterialrendererfor loop

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.

alt text

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?

capture.png (37.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Material class use general question 1 Answer

appear/disappear gameobject 3 Answers

Changing material of objects by the tag in different scenes 0 Answers

Rendermode: Cutout not working on gameobject from external asset bundle 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges