Search Unity

Javascript to change the tint color of a particles/additive shader?

Discussion in 'Scripting' started by QuinnWinters, Feb 1, 2015.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    In javascript, how would I change the tint color of a particles/additive shader?

    When I change the color of a standard material I do something along the lines of the following:
    Code (JavaScript):
    1.         colorR = Random.Range (0, 255);  //Randomize the rgb colors
    2.         colorG = Random.Range (0, 255);  //Randomize the rgb colors
    3.         colorB = Random.Range (0, 255);  //Randomize the rgb colors
    4.         myObject.renderer.material.color = Color32(colorR, colorG, colorB, 1);  //Set the material color
    In my research I found that a tint color can't take RGB values and needs values between 0 and 1, but I can't figure out how to convert an RGB value into whatever format the 0-1 value would be called. I've also found that render.material.tintcolor is not valid, so I have no idea how to even access the tint color once I can convert the RGB values. I've read that renderer.material.SetColor("_TintColor", myColor) is possibly the proper way to go about it but I tend to get errors with every variation of that I try, though I don't doubt the errors may be caused by not having the RGB value in the proper format.

    Any suggestions on how I can accomplish this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Renderer.material.color is just a shortcut for Renderer.material.SetColor("_Color"), so if the shader uses something other than _Color, you need to use the correct name with SetColor. Nothing to do with 0-1 vs 0-255, which is the difference between Color and Color32 structs (which are mostly interchangeable).

    --Eric
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Thanks for the quick reply, that helped a lot. I've come up with the following which seems to work:

    Code (JavaScript):
    1. myObject.renderer.material.SetColor("_TintColor", Color32(colorR, colorG, colorB, 128));