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. Dismiss Notice

Trouble assigning colors to objects

Discussion in 'Scripting' started by Recklezz, Apr 12, 2016.

  1. Recklezz

    Recklezz

    Joined:
    Apr 12, 2016
    Posts:
    2
    Hello xD

    What i am trying to create is a board in which you can assign colors to different objects. The goal is to have a way to change these colors first through a list and then assign these colors to multiple objects. What i've got trouble with is the assign / render part of the objects. I am new at unity, and it is a very abstract mission i've set for myself.

    Code (CSharp):
    1. public class ScriptAPI : MonoBehaviour {
    2.     public GameObject[] color1;
    3.     public GameObject[] color2;
    4.     public GameObject[] color3;
    5.     public GameObject[] color4;
    6.     public GameObject[] color5;
    7.  
    8.     void Start () {
    9.         color1 = GameObject.FindGameObjectsWithTag ("Model1");
    10.         color2 = GameObject.FindGameObjectsWithTag ("Model2");
    11.         color3 = GameObject.FindGameObjectsWithTag ("Model3");
    12.         color4 = GameObject.FindGameObjectsWithTag ("Model4");
    13.         color5 = GameObject.FindGameObjectsWithTag ("Model5");
    14.  
    15.         foreach (GameObject Model1 in color1) {  
    16.             // don't know what to write to change the material color.
    17.         }
    18.         foreach (GameObject Model2 in color2) {        }
    19.         foreach (GameObject Model3 in color3) {        }
    20.         foreach (GameObject Model4 in color4) {        }
    21.         foreach (GameObject Model5 in color5) {        }
    22.     }
    23.  
    24.     void Update () {
    25.    
    26.     }
    27. }
    28.  
    The code i've made is very amateurish and i am really a beginner at coding, feel free to write completely new code to solve the problem
    Any help is appreciated
     
  2. gheeler

    gheeler

    Joined:
    Nov 7, 2012
    Posts:
    18
    Code (CSharp):
    1.  
    2. foreach (GameObject Model1 in color1) {
    3.     Model1.GetComponent<Renderer>().material.color = Color.red; // etc
    4. }
    5.  
     
    Not_Sure and Recklezz like this.
  3. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    You can also manually punch in RGB values.
     
  4. Recklezz

    Recklezz

    Joined:
    Apr 12, 2016
    Posts:
    2
    The problem is was having, is that the color should be dependent on values gotten from a arduino, and the colors just wouldn't work.