Changing Material Color Via Script

So I think I actually have a simple problem. I have this script portion of a script:

//Stretch arrow.
if (Distance < MaxPull) {

var r : int = (255 / MaxPull * Distance);
var b : int = (255 - (255 / MaxPull * Distance));

ActualArrow.renderer.enabled = false;
MaterialColor.SetVector("_Color", Vector4(r, 0, b, 1)); 
ActualArrow.renderer.enabled = true;
WholeArrow.animation["Arrow Animation"].time = Distance * (WholeArrow.animation["Arrow Animation"].clip.length / MaxPull);
WholeArrow.animation["Arrow Animation"].speed = 0.0;
WholeArrow.animation.Play("Arrow Animation");


I’m trying to change the color of an object based on how far the mouse cursor’s ray is away from it. Anyway, I went through a billion different problems. At first I was trying to animate the colors which didn’t seem to work and gave the object a pink color. In fact, that’s what’s happening now, but I checked the material color after pausing the game and the numbers, instead of 0 - 255, are up in the thousands.

The variables you see above, r and b, are ALWAYS between 0 - 255. Mathematically it’s impossible for them to be above or below. So I figured maybe the material was taking float values like 149.652 and using them as ints by removing the decimal point.

SO I went and isolated the variables, made them an int, did a print to the console thingy, and still no go for some reason. The values are ridiculously high. That’s why the color becomes this pink color (I’m colorblind so it very well could be purple).

To put everything into a nutshell:

Need a value between 0 - 255 which seems to be working.

Print returns values such as 149.000 now that everything’s int (Maybe those zeros are a problem?).

Object becomes pink and the material RGB values while checked in the inspector are high as f.

I tried various methods of changing the color as well.

I know there are a few bugs with changing materials or something in the new Unity so I don’t know if that’s it or not. I hope I gave you enough information and that I’m not asking a really simple question :slight_smile:

Thanks in advance, as always.

A couple of comments. The RGB values the Color class are float values between 0.0 and 1.0, not values in the range of 0 to 255. There is a Color32 class in Unity that takes colors in that range, but the shader takes the Color class.

In addition, since you are trying to assign to _Color, you can the shortcut to directly assign the color.

MaterialColor.color = new Color(0.5, 0.0, 0.75, 1.0);

Actual color changes will depend on the shader.