Search Unity

Can't use GetColor to store colour from object & can't SetColor with any Alpha

Discussion in 'Getting Started' started by Shin_Toasty, Apr 5, 2018.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    All I want to do is change an object's colour on collision for a split-second, which I've coded OK - but GetColor doesn't get the original colour from the object (to go back to) and I can't set colours with any Alpha, they are all opaque no matter what values I key in, and whether I use Color or Color32.

    The object made of tinted glass, I just want it to flash a different colour then go back to its original one. Have been trying:

    Code (CSharp):
    1.              public Color32 tint = new Color32 (255, 200, 0, 0);
    2.  
    3.              rend.material.SetColor("_Color", tint);
    Those 4 values are random. rend is my renderer. Should I use Color32 or not?

    Got no error messages but I have to reload the whole project file every time I change the colour values - or Unity just displays the object in the old colours.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Whether alpha does anything or not is up to the shader. Chances are you're using a shader that's not set up to support transparency.

    I don't know what you mean that GetColor doesn't return the original color from the object. Can you elaborate?

    You should use Color, not Color32 in this case. (Check the docs for yourself.)

    I also don't know what you mean about having to reload the whole project file. Please use more words, or fewer words chosen more carefully.
     
    Shin_Toasty and Bill_Martini like this.
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    The forth argument in the new color call is the alpha channel. Zero is invisible and probably not what you want. If you're just tinting an object temporarily use a value above zero. Note: all values used in Color are floats with values from 0.0f - 1.0f.
     
    Shin_Toasty likes this.
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For the regular colour, you can just use '.color', also. Assign the return value of that to your colour, do your change, and use the variable to assign the value back.
    As for Color or Color32, I think it won't matter, as they have implicit conversions.
     
    Shin_Toasty likes this.
  5. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Yes, that's it. I was accidentally using a legacy shader because I'd pasted in a line of code from the Scripting API.

    The problem with Unity not updating the game until I reloaded the project was real: could be a bug, but not a big problem as it doesn't happen with non-legacy shaders.

    Edit: actually, when I get the colour, store and reapply it, I get a cross-hatch pattern, here it is shown at 5x. With the normal material I use Standard Shader, rendering mode Transparent. Do I need to capture any other info besides colour from the material?

    crosshatch.gif
     
    Last edited: Apr 6, 2018