Storing Colors in a Variable?

Back again!

I’m trying to grab a random color and assign it to a material (VertexLit). For the most part, I believe I have this figured out. However, I’m having issues storing my color in a variable.

Here’s where I call the RandomColor function:

aColor = RandomColor();

This is the RandomColor function

function RandomColor(){

	var col;
	c=Mathf.FloorToInt(Random.value*6);
	switch(c){
    	case 0:
    		col=Color.red;
    		break;
    	case 1:
    		col=Color.blue;
    		break;
    	case 2:
    		col=Color.green;
    		break;
    	case 3:
    		col=Color(1.0,0.5,0.0);//orange
    		break;
    	case 4:
    		col=Color.yellow;
    		break;
    	case 5:
    		col=Color(0.8,0.0,0.8);//purple
    		break;
    }
    return col;
}

And here is where I color my object:

	b=Instantiate(bullet, transform.position + Vector3.right*2, Quaternion.Euler(0, 0, 90));
	b.renderer.material.color=aColor;

I’m getting the error that “aColor is not a member of UnityEngine.Color”

I’ve also tried declaring aColor, as:

var aColor:Color;

But that doesn’t work either…

Can you post the script in its entirety, along with the exact error message and an indication of which line generates the error?

Also, your RandomColor() function can be shortened up a bit, e.g.:

function RandomColor()
{
	switch(Random.Range(0, 6)) {
    	case 0:
    		return Color.red;
    	case 1:
    		return Color.blue;
    	case 2:
    		col=Color.green;
    	case 3:
    		return Color(1.0,0.5,0.0);//orange
    	case 4:
    		return Color.yellow;
    	case 5:
    		return Color(0.8,0.0,0.8);//purple
        default:
                // Throw an exception maybe...
    }
}

Edited

Also, I couldn’t shorten that switch case because I need it to check whole numbers, not floats. I suppose I could use Range, but I’d still have to round it down. I could also remove the breaks, but that’s just good practice to keep things like that nice and neat. I may add the default though… cuz I don’t trust computers…

Oh, I just noticed you put the return values in the in the Case statements… except for green… I suppose I could do that too.

Have you tried using a breakpoint and inspecting aColor before and after you call aColor = RandomColor();?

If you don’t use Monodevelop (and can’t debug it) you can always do something like…

var aColor : Color = Color.black;

Debug.Log("aColor Black: " + aColor);

aColor = RandomColor();

Debug.Log("aColor Random: " + aColor);


Sorry I can’t be of more help, I only work in C# (go go type safe variables :smile:).

For those that work with UnityScript - there is a “#pragma strict” or something he can place at the top of his file to force type safe variables right? Maybe enable that and see what kind of errors the complier gives you.

Which line generates the error?

It does check whole numbers. Maybe there’s something different you have to do in UnityScript to make it work, but the version of Random.Range() in question returns an integer in the range [min, max), which is exactly what you want.

See above.

Oops, my bad :wink:

But yeah, as long as all you’re doing is returning the value, you can just return directly after the ‘case’ statement as in my example.

This is the line throwing the error.

		b.renderer.material.color=aColor;

I commented out the line that assigns the color to the material and put in a debug to find what was stored in aColor. This is what it gave me:

aColor: RGBA(0.800, 0.000, 0.800, 1.000)

so… purple.

Yeah, I have no idea what that error is trying to tell you (maybe it’s obvious, but I’m just not seeing it).

I’m afraid I’m going to have to leave this one to the UnityScript experts.

Ok, I managed to get it to work using

b.renderer.material.SetColor(“_Color”,aColor);

To my understanding, this should work the exact same way. I think the previous method may need to be looked into. I’m just happy I got it working. Thanks again, everyone!

Yeah, the two are essentially equivalent, AFAIK, so I’d be interested as well in knowing why your original code didn’t work.