# How to change material's color in blender game engine?

I am making a new racing game in BGE. I want my game to have a feature to change car color in-game (car consists of multiple materials, but currently I want to change body material). How do I make something like this:

Without text (only color select) in blender, so it can apply on material in other scene?

First you have to enable Object Color, that is found in the Properties window, Material tab, then under the Options region Object Color.
One quirk I found, be sure that the diffuse color of the material is pure white [1, 1, 1]. If it is not you will only be able to control the color up to that level. Example: say the material's color is red [1, 0, 0], now because the green and blue channels are 0 you will never be able to get any value in those channels.
Now the material will use the object's color, that color is found under Properties > Object > Display > Object Color*.

To change the object color while the game is running use this bit of python.

cont = bge.logic.getCurrentController()
obj = cont.owner
# set the game object's color
obj.color = [ 0.0, 0.0, 0.0, 1.0]


The color method is looking for a vector in the form of [Red, Green, Blue, Alpha], (You can just leave the alpha set to 1, I have yet to find a place where it is used.)

## The game UI for changing the color

Creating the interface is the most complicated part.
Here is a working demo file with three sliders, one each for red green and blue. The three sliders all have a game property one for each color channel. They also have these logic bricks. The only difference among the three is the property name.

This script is what takes the input from the logic bricks above, and changes the three values.

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

if cont.sensors['Mouse'].getButtonStatus(bge.events.LEFTMOUSE) == bge.logic.KX_INPUT_ACTIVE:
posZ = cont.sensors['Mouse1'].hitPosition[2]
if posZ > 1.0:
posZ = 1
elif posZ < 0:
posZ = 0
own.worldPosition.z = posZ

prop = cont.actuators["prop"]
prop.value = str(own.worldPosition.z)
cont.activate(prop)


Then another script on the object you want to change the color is this script. A always sensor set to True pulse mode makes the script run constantly.

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

r = scene.objects['handle']['r']
g = scene.objects['handle.g']['g']
b = scene.objects['handle.b']['b']

own.color = [ r, g, b, 1.0]


If you are ok with having a few set colors, then there is a way to have color changing in the BGE with no python.

First duplicate your object that should have the different colors, move it to a different layer. Duplicate it once for each new color, and give each a new materiel. Change the color of each material (duh). Rename the Mesh data so you can keep track of which one has what color.

Now back on the real object, add one Message sensor, and one Edit Object actuator for each of the different colors you created for your object, like the logic bricks below. You should set the different colored mesh data (this is why you should of named them) in the Mesh field of the Edit Object actuator.

Finally create a menu like this to hold the buttons that will change the color. You will need a button for each color. Give each button logic bricks like in the image below. The subject in the Message actuator is important, that is how the button changes the color.

When you click on one of the buttons it send a message with the color. The message sensor in the object that will get its color changed, reads the color from the subject line, and triggers the correct Edit Object actuator.

Blend file

(Having come back to this question to fix dead links) I now have another method to change an objects color in game without python.

This method does not use duplicated object data, nor any python.

Start by animating the color of the material. I used 7 key frames placed at the major colors around the color wheel. Just choose the saturation and value, then use the hue slider to change the color. This way you get an animation all around the color wheel ,see gif below.

Please pardon the terrible dithering in the gif...

With that animated now on to the logic bricks. I will skip the game buttons because they are exactly the same as the buttons I made in this answer

First we need a game property. I called mine "frame," whatever its name make it an integer.
Now the magic happens in the logic bricks behind the main object.

Start off with an Always sensor, with true level triggering enabled. Plug that into an Action actuator. This action actuator is what controls the animation we made earlier.
Be sure to set the Action Playback Type to Property.
Set the Property field to the one created earlier (I used "frame").
Last set the Action name to the animation we made first. If you did not rename it, it will be named "Your_Material_NameAction".

At this point you can change the color of the material by changing the value of the game property.

All that is left to do is, tie the message that each of the buttons send to a value. Example from my animation (as can be seen in the gif) I have red at frame 0, blue as 64, yellow is 13 etcetera.

To do that add a Message sensor, the subject is from one of the buttons, made earlier. To be logical (remember we are using logic bricks) I named the subject of the message my yellow button sends "yellow." Hence the message sensor listens for "yellow."

Plug that into a Property actuator.
Set the Mode to Assign, the Property to the game property made earlier (remember I called mine "frame") and the Value to the frame that corresponds to a yellow color.

Repeat for each color you want, and your done.

The full blend