Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Location
    Kemptville, Ontario
    Posts
    6,238

    changing color in Unity.

    I can usually stumble my way through this stuff but for some odd reason this has me stumped.

    I wish to change the color of an object in code using c#.

    Now this works:
    Code:
    renderer.material.color = Color.red;
    What I want though is to use RGBA values instead because I will be changing them during game play. It is probably obvious but my tired little brain can't figure it out right now.

    Thank you.
    I have figured out what the internet is. You know what they say about an infinite number of monkeys hitting keys at random on an infinite number of typewriters for an infinite amount of time? Well the internet is like that. There is some big cosmic alien running that experiment here in order to find the answer to some big question or to create the greatest piece of literature known to the universe. From what I have seen so far I am sorry to say they are going to be greatly disappointed.

  2. #2
    Join Date
    Aug 2002
    Location
    Vancouver Island
    Posts
    59
    should be able to use:

    Code:
    renderer.material.color = (1,1,1,1);
    where the values are from 0 to 1.

  3. #3
    Quote Originally Posted by dangerdave99 View Post
    should be able to use:

    Code:
    renderer.material.color = (1,1,1,1);
    where the values are from 0 to 1.
    Oh weird. I've been thinking this whole time that Color was 0-255, but it's 0-1 instead?

    And of course it's C#, so you'll want to do renderer.material.color = new Color(1, 1, 1, 1);

    There's also an overload without the alpha.
    Last edited by Raziaar; 01-24-2010 at 03:43 AM.
    Hello everyone and welcome to another 3DBuzz VTM, and tonight we'll be programming in Brain****!

  4. #4
    Join Date
    May 2004
    Location
    UK
    Posts
    708
    Quote Originally Posted by Raziaar View Post
    Oh weird. I've been thinking this whole time that Color was 0-255, but it's 0-1 instead?
    I've only ever tried changing the colour of a GUITexture in code, and that certainly uses RGB values and not 0 - 1 values...

    Code:
    this.guiTexture.color = new Color(255f, 255f, 255f, 1);
    Cheers

    Mat

  5. #5
    Join Date
    Dec 2007
    Location
    Austin, Texas
    Posts
    499
    it really varies... some things are from 0-255, some from 0-1. material colors are in 0-1, just like in Unreal. GUI in general seems to be the previous, 0-255, which throws people off. now, the funny thing is... is that sometimes, i believe, 0-255 works for material colors also, and vice versa for GUI colors.

    So really, I think either way works.... its just a silly mechanism that I believe was built to confuse people.

  6. #6
    Join Date
    Jan 2004
    Location
    Kemptville, Ontario
    Posts
    6,238
    Thank you everyone. That worked. Well it worked once I actually started to think and finally switch my material to Transparent->Diffuse. Yes there was about five minutes of self imposed confusion.

    What confused me about color change is all the different yet poorly documented methods to do this.
    I have figured out what the internet is. You know what they say about an infinite number of monkeys hitting keys at random on an infinite number of typewriters for an infinite amount of time? Well the internet is like that. There is some big cosmic alien running that experiment here in order to find the answer to some big question or to create the greatest piece of literature known to the universe. From what I have seen so far I am sorry to say they are going to be greatly disappointed.

  7. #7
    Join Date
    Feb 2010
    Posts
    1

    I am confused too :)

    I am completely new to Unity, and currently struggling to find good tutorials / instructions for doing anything. I have a bouncing sphere in my code. It collides with different cubes when it bounces. There are 16 of them in 4 rows/columns. However, I wish to change the color of each cube the sphere collides with, when it does.

    I have no idea how to change color. Also do I add a OnTriggerEnter function to the ball, so that I can change the color of the 'other' collider?

    In my thinking adding color changing script to 'OnTriggerEnter' might work?

    Is it possible for anyone to help me make my sphere change the colors of cubes?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •