in the attached file below, the right cube is displayed red in 3D view and in the render result:
However, if you press the P key, the right cube is displayed green:
I expect it to be red. Why is that?
Note: Both object use the same mesh. The mesh has one color (red). The right object uses the mesh’s color (“data”) and has no color in the object’s material list. The left mesh uses the object’s color which is green.
The right cube has no reference to a green color, so I think this is not correct display.
It is because your cubes share the exact same mesh data. Select one of the cubes, go into “Object Data” (the triangle thing) and click “2” next to “Cube” to make it a single user. This will change the name to Cube.001. The render it or go into game mode and they will be different colors.
Yes, they share the same mesh. That’s intended. Considering the right cube, it has no relation to a green color, neither from the mesh nor from the object. So it must not be green.
You are not understanding how the mesh data is converted when the game engine starts. Both objects share the same mesh data, so in the game engine there is only one mesh datablock. This is very important. There is only one mesh object that is shared between all instances of that mesh.
When the scene is being converted, the base mesh material (the red color) is applied to the mesh object. However, when Cube.001 is processed, the scene converter applies the additional material (the green color) to the mesh object. The single mesh object becomes green, and this causes all instances of that mesh to be displayed as green.
This is not a bug, but rather a difference in the way that the game engine handles instanced object data. I also ran your .blend file in OgreKit, and the behavior is the same in gamekit as it is in blender’s game engine.
The simple answer is this: If you want two objects to have different colors, you will need to use separate meshes. That’s just the way it is.
Edit:
Below is an image of both cubes displaying green in gamekit on my kids’ android tablet.
So, in short, due to limitations in the game engine, the materials are not used as in the render engine. That’s good to know. It is not obvious through the UI.