Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom color script not working

Discussion in 'Scripting' started by CarltonSmith, Jan 19, 2014.

  1. CarltonSmith

    CarltonSmith

    Joined:
    Jan 19, 2014
    Posts:
    21
    I did a search on how to change colors of simple objects like a cube and i managed to do it using the unity tutorial on scripting and as expected this works great and as expected:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChangeColorScript : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if(Input.GetKeyDown(KeyCode.R))
    14.         {
    15.             gameObject.renderer.material.color = Color.red;
    16.         }
    17.         if(Input.GetKeyDown(KeyCode.G))
    18.         {
    19.             gameObject.renderer.material.color = Color.green;
    20.         }
    21.         if(Input.GetKeyDown(KeyCode.B))
    22.         {
    23.             gameObject.renderer.material.color = Color.blue;
    24.         }
    25.     }
    26. }
    This code is currently attached to a cube with a rigidbody. There are no other things in the scene except a camera, a plane and a light.

    But if i wanted to use my own RGB values how would i go about this. After hours of experimentation and numerous searches i cant seem to find the answer. One would assume that this works:

    Code (csharp):
    1.  
    2. gameObject.renderer.material.color = Color(0.777, 08, 0.604);
    3.  
    As it is taken from here : http://solution2tech.blogspot.in/2013/10/change-material-color-in-unity-3d.html. And i even added the alpha value too in case that was the issue.

    I figured i would be able to either initialise the current color using this or replace the "R" if statement to make it change to this "custom" color but unfortunately i cant seem to work out where i am going wrong.

    Please help. Thanks.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    If this is C# (let's assume it is), it should be;

    Code (csharp):
    1.  
    2. gameObject.renderer.material.color = new Color(0.777f, 0.8f, 0.604f);
    3.  
    "new" is the keyword to create a new instance of that struct/class.

    Color's constructor takes 3 or 4 float, depending if you pass the alpha or not.

    Float, in C#, ends with the letter "f" to differentiate them from double, which you could guess it, finish with a "d".
     
  3. CarltonSmith

    CarltonSmith

    Joined:
    Jan 19, 2014
    Posts:
    21
    Thank you so much. Yes i should have stipulated that it was c# to begin with, i wasnt thinking :)

    Just as an addition to this, i dont suppose there is a way to use the traditional RGB values 0-255 so i could use a color picker to grab the exact value i wanted as opposed to 'guessing' the float values (obviously 0 is 0 and i presume 0.5 is in the middle of 255 ish but i'd like to just use a color picker in case i need a more accurate representation).

    I know this is a different question but as its related somewhat i dont want to clutter things up and start a new thread.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Well yes you can.

    Code (csharp):
    1.  
    2. gameObject.renderer.material.color = new Color32(128, 128, 128, 255);
    3.  
    Color32 takes bytes (0-255) instead of float. However, there's only one constructor so you have to give it the alpha.

    Color and Color32 can be implicitly converted from one to the other.
     
    Ptb likes this.
  5. CarltonSmith

    CarltonSmith

    Joined:
    Jan 19, 2014
    Posts:
    21
    Oh thats brilliant, and nice and simple.

    Thanks once again for your help.