Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why when coloring in script some objects it's coloring all the objects ?

Discussion in 'Scripting' started by Chocolade, Aug 16, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1.  
    2. for (int i = 0; i <  listfoundobj.Count; i++)
    3.         {
    4.             listfoundobj[i].GetComponent<Renderer>().sharedMaterial.color = Color.green;
    5.         }
    6.  
    listfoundobj contain 4 items.

    And i have a grid of objects size 10x10 and it keep coloring the whole grid the whole 100 objects but i want it to color only this 4 objects.
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Now it's coloring them using only material. But also give me exception:

    Code (csharp):
    1.  
    2. listfoundobj[i].GetComponent<Renderer>().material.color = Color.green;
    3.  
    Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.