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

Altering Material colour RGB at runtime problems

Discussion in 'Scripting' started by Select_Casey, Mar 27, 2015.

  1. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    Hi again <3

    I've run into some problems trying to alter the "Main Color" RGB values at runtime. The script i wrote doesnt appear to behave as expected and i was wondering if someone could offer an explanation and/or a fix.

    I have a plant model, i want to change its colour when it starts based on rgb values. When i try to pass values to the RGB, unity seems to be altering my values, but not in the alpha channel for some reason. when i pass it the value "255" all channels except alpha are filled with "65025" instead which corrupts the colour.

    Here is my script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class plants2 : MonoBehaviour {
    5.     public  byte redPigment=255;
    6.     public  byte greenPigment=255;
    7.     public  byte bluePigment=255;
    8.  
    9.     void Start () {
    10.         Renderer theRenderer = GetComponent<Renderer>();
    11.         Color TheHue = new Color (redPigment, greenPigment, bluePigment, 255);
    12.         theRenderer.material.SetColor("_Color", TheHue);
    13.     }
    14.  
    15.  
    16. }
    17.  
    Why is it not passing my numbers correctly? i tried replacing redPigment etc with just the numbers, but it returns the same result. Oh and i tried using floats instead of bytes first, but that changes nothing too
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Use floats in the Color constructor with 0 to 1 values
     
    Select_Casey and Fajlworks like this.
  3. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    You beauty!!! Thanks, that was making me want to drink bleach.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Also for basic colors white,black, red green blue etc, Color has some static colors in it.

    Like Color.blue
     
  5. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    yeah, i knew about those from documentation. i specifically needed this code to be for RGB as its meant to simulate pigment drift in plants over several generations.
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Oh good, just mentioned it since your example was using white.
     
  7. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    yeah, the white is just to give the plant texture no pigment other than its base green as default. i figure chlorophyll doesnt change colour, at least not for the purposes of my sim anyway