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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Changing Object Colour

Discussion in 'Scripting' started by shodaica, Sep 2, 2018.

  1. shodaica

    shodaica

    Joined:
    Sep 18, 2017
    Posts:
    29
    I am working on a matching game and have been able to lay out everything as I want it and have created an array of objects to hold their attributes. However, now I am stuck. I want to be able to change the colour of the objects on the screen. I am using a simple Mario image found on google, and have looked up and found a number of different answers, however, I have had no success. At this point only the default Sprite Renderer is attached and I have not changed any settings.

    Here is the code I have been working with.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameSetup : MonoBehaviour {
    6.  
    7.     public int boxX = 200;
    8.     public int boxY = 275;
    9.  
    10.     public int marioX = 200;
    11.     public int marioY = 275;
    12.  
    13.     public int numBlue = 0;
    14.     public int numRed = 0;
    15.     public int numGreen = 0;
    16.  
    17.     public int numObjects = 9;
    18.  
    19.     public GameObject[] boxes;
    20.     public GameObject box;
    21.  
    22.     public GameObject[] marios;
    23.     public GameObject mario;
    24.  
    25.     public Color[] pickColour;
    26.  
    27.     void Start() {
    28.         //SetColours ();
    29.         PlaceBoxes ();
    30.         PlaceMarios ();
    31.     }
    32.  
    33.     public void SetColours(){
    34.         pickColour = new Color[numObjects];
    35.         for(int i = 0; i < numObjects; i++){
    36.             int ranNum = Random.Range (1,5);
    37.             if (ranNum == 1){
    38.                 if (numBlue <= 3){
    39.                     numBlue += 1;
    40.                     pickColour[i] = Color.blue;
    41.                     print (i + " = blue");
    42.                 }
    43.                 else{
    44.                     i -= 1;
    45.                 }
    46.             }
    47.             else if (ranNum == 2){
    48.                 if (numRed <= 3){
    49.                     numRed += 1;
    50.                     pickColour[i] = Color.red;
    51.                     print (i + " = red");
    52.                 }
    53.                 else{
    54.                     i -= 1;
    55.                 }
    56.             }
    57.             else if (ranNum == 3){
    58.                 if (numGreen <= 3){
    59.                     numGreen += 1;
    60.                     pickColour[i] = Color.green;
    61.                     print (i + " = green");
    62.                 }
    63.                 else{
    64.                     i -= 1;
    65.                 }
    66.             }
    67.             else if (ranNum == 4){
    68.                 i -= 1;
    69.             }
    70.         }
    71.     }
    72.  
    73.     public void PlaceBoxes(){
    74.         boxes = new GameObject[numObjects];
    75.         for (int i = 0; i < numObjects; i++){
    76.             if (i < 3) {
    77.                 GameObject placeB = Instantiate (box, new Vector3 (boxX,boxY,10f), Quaternion.identity);
    78.                 boxes[i] = placeB;
    79.                 boxX += 100;
    80.                 if (i == 2){
    81.                     boxY -= 100;
    82.                     boxX -= 300;
    83.                 }
    84.             }
    85.             else if (i >= 3 && i <= 5) {
    86.                 GameObject placeB = Instantiate (box, new Vector3 (boxX,boxY,10f), Quaternion.identity);
    87.                 boxes[i] = placeB;
    88.                 boxX += 100;
    89.                 if (i == 5){
    90.                     boxY -= 100;
    91.                     boxX -= 300;
    92.                 }
    93.             }
    94.             else if (i > 5) {
    95.                 GameObject placeB = Instantiate (box, new Vector3 (boxX,boxY,10f), Quaternion.identity);
    96.                 boxes[i] = placeB;
    97.                 boxX += 100;
    98.             }
    99.         }
    100.     }
    101.  
    102.     public void PlaceMarios ()
    103.     {
    104.         marios = new GameObject[numObjects];
    105.         for (int i = 0; i < numObjects; i++) {
    106.             if (i < 3) {
    107.                 GameObject placeM = Instantiate (mario, new Vector3 (marioX, marioY, -1f), Quaternion.identity);
    108.                 marios [i] = placeM;
    109.                 marioX += 100;
    110.                 if (i == 2){
    111.                     marioY -= 100;
    112.                     marioX -= 300;
    113.                 }
    114.             }
    115.             else if (i >= 3 && i <= 5){
    116.                 GameObject placeM = Instantiate (mario, new Vector3 (marioX, marioY, -1f), Quaternion.identity);
    117.                 marios [i] = placeM;
    118.                 marioX += 100;
    119.                 if (i == 5){
    120.                     marioY -= 100;
    121.                     marioX -= 300;
    122.                 }
    123.             }
    124.             else if (i > 5){
    125.                 GameObject placeM = Instantiate (mario, new Vector3 (marioX, marioY, -1f), Quaternion.identity);
    126.                 marios [i] = placeM;
    127.                 marioX += 100;
    128.             }
    129.         }
    130.         //SetColours ();
    131.     }
    132. }
    133.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SetColours : GameSetup {
    6.  
    7.     //Renderer rend;
    8.  
    9.     void Start () {
    10.         //rend = GetComponent<Renderer>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.     }
    17.  
    18.     public void SetColour(){
    19.         for (int i = 0; i <= numObjects; i++){
    20.             //rend.material.SetColor ("_Colour", pickColour[i]);
    21.             transform.GetComponent<Renderer> ().material.color = pickColour[i];
    22.         }
    23.  
    24.     }
    25. }
    26.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
  3. shodaica

    shodaica

    Joined:
    Sep 18, 2017
    Posts:
    29
    Ok, so I have played around with the code, but I am running into two issues. The first may be because I do not fully understand what is happening. When I run the code fully the colour changes to a dark almost black colour. But if I remove the last two lines the colour changes to pink. Not exactly sure why that is happening.

    The second issue is I have created 9 pictures based on a prefab. I have kept each object in an array of Objects and I have the colours selected in an array of Colors. However I am not sure have to combine them.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Pink color is used typically, when no color exists. For example is invalid.
    Regarding Color the part you may be confused, is the values of colors, providing you not using presets colors, like Color.red, Color.green etc.
    If you set any of RGB or RGBA values to 255, the color may be not accepted.
    Just in case, any color and alpha range should be float 0.0f to 1.0f.

    Code (CSharp):
    1. //Attach this script to a GameObject that has a Renderer component
    2.  
    3. using UnityEngine;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         //Fetch the Renderer from the GameObject
    10.         Renderer rend = GetComponent<Renderer>();
    11.  
    12.         //Set the main Color of the Material to green
    13.         rend.material.shader = Shader.Find("_Color");
    14.         rend.material.SetColor("_Color", Color.green);
    15.  
    16.         //Find the Specular shader and change its Color to red
    17.         rend.material.shader = Shader.Find("Specular");
    18.         rend.material.SetColor("_SpecColor", Color.red);
    19.     }
    20. }
    In start, keep for testing purpose
    Code (CSharp):
    1. Renderer rend = GetComponent<Renderer>();
    and
    Code (CSharp):
    1. rend.material.SetColor("_Color", Color.green);
    Remove, or comment out the rest.
    For now, ignore shader. Normally you wont need it for simple color change.
    If that fails, try replace SetColor attributes with
    Code (CSharp):
    1. rend.material.SetColor("_Albedo", Color.green);
    Is the property name used in shaders for base material colors.

    Tell me if you managed to get anywhere with that.
     
    shodaica likes this.
  5. shodaica

    shodaica

    Joined:
    Sep 18, 2017
    Posts:
    29
    Ok, so there is some progress. However, I am still a little confused. It is changing the color of the picture to green. However, it is not changing the color of all of the other objects that are made from the prefab. This my code as it stands now. Also, just an FYI, when I try changing the color from green to an element from my array it does not work.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SetColours : GameSetup {
    6.  
    7.     public GameObject[] marios;
    8.     public GameObject mario;
    9.  
    10.     public int boxX = 200;
    11.     public int boxY = 275;
    12.  
    13.     void Start () {
    14.         Renderer rend = GetComponent<Renderer>();
    15.         rend.material.SetColor("_Color", Color.green);
    16.         SetupColours ();
    17.         PlaceBoxes ();
    18.         PlaceMarios ();
    19.     }
    20.  
    21.     public void PlaceMarios ()
    22.     {
    23.         print("Hello");
    24.         //Renderer rend = GetComponent<Renderer>();
    25.         marios = new GameObject[numObjects];
    26.         for (int i = 0; i < numObjects; i++) {
    27.             if (i < 3) {
    28.                 //rend.material.shader = Shader.Find("_Color");
    29.                 //rend.material.SetColor("_Color", pickColour[i]);
    30.                 GameObject placeM = Instantiate (mario, new Vector3 (boxX, boxY, -1f), Quaternion.identity);
    31.  
    32.                 marios [i] = placeM;
    33.                 boxX += 100;
    34.                 if (i == 2){
    35.                     boxY -= 100;
    36.                     boxX -= 300;
    37.                 }
    38.             }
    39.             else if (i >= 3 && i <= 5){
    40.                 GameObject placeM = Instantiate (mario, new Vector3 (boxX, boxY, -1f), Quaternion.identity);
    41.                 marios [i] = placeM;
    42.                 //rend.material.shader = Shader.Find("_Color");
    43.                 //rend.material.SetColor("_Color", pickColour[i]);
    44.                 boxX += 100;
    45.                 if (i == 5){
    46.                     boxY -= 100;
    47.                     boxX -= 300;
    48.                 }
    49.             }
    50.             else if (i > 5){
    51.                 GameObject placeM = Instantiate (mario, new Vector3 (boxX, boxY, -1f), Quaternion.identity);
    52.                 //rend.material.shader = Shader.Find("_Color");
    53.                 //rend.material.SetColor("_Color", pickColour[i]);
    54.                 marios [i] = placeM;
    55.                 boxX += 100;
    56.             }
    57.         }
    58.     }
    59. }
    60.  
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    So if I understand, you want set mario color, as prefab, from which you later make copies.

    You need modify line 14 from
    Code (CSharp):
    1. Renderer rend = GetComponent<Renderer>();
    to
    Code (CSharp):
    1. Renderer rend = mario.GetComponent <Renderer>() ;
    Basically
    Code (CSharp):
    1. Renderer rend = GetComponent<Renderer>();
    is equivalent to
    Code (CSharp):
    1. Renderer rend = this.GetComponent<Renderer>();
    Where this is a reference, to an instantiated class SetColours.
    Is expecting to have Renderer with material.
    You don't have any error by any chance?
     
    shodaica likes this.
  7. shodaica

    shodaica

    Joined:
    Sep 18, 2017
    Posts:
    29
    When I change the code as suggested, the prefabs do not display and I get the following error:
    Not allowed to access Renderer.material on prefab object. Use Renderer.sharedMaterial instead
    UnityEngine.Renderer:get_material()
    SetColours:Start() (at Assets/Scripts/SetColours.cs:15)
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Try out of interest
    Code (CSharp):
    1. MeshRendererrend = mario.GetComponent <MeshRenderer> () ;
    If that fails, try change

    Code (CSharp):
    1. rend.material.SetColor("_Color", Color.green);
    to
    Code (CSharp):
    1. Material mat = rend.material ;
    2. mat.SetColor("_Color", Color.green);
    See where we get with it.
     
    shodaica likes this.
  9. shodaica

    shodaica

    Joined:
    Sep 18, 2017
    Posts:
    29
    I got it. I added the following function and it seems to work perfectly.
    Code (CSharp):
    1.     public void ColourMario(){
    2.         for(int i = 0; i < numObjects; i++){
    3.             Renderer rend = marios[i].GetComponent <Renderer>();
    4.             rend.material.SetColor ("_Color", pickColour[i]);
    5.         }
    6.     }
    Thank you soooooo much. This was a big challenge for me and I really appreciate your patience and help.
     
    Antypodish likes this.
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    You welcome.