- Home /
Detecting color match
Is there a way for Unity to detect color matches between assets? Please refer to the image below. Notice that the two red A letters are on opposite sides.
In the image below notice that the two red A letters are matched. Is there a way to get Unity to detect that the colors match?
There's nothing special in Unity that does this, but there are a number of different ways it can be solved. How difficult or easy it might be will depending on a lot of factors. You need to give a more detailed explanation of your app. Things like:
2D or 3D?
Number of tiles
Are the tiles placed by the user?
Are the tiles moved by the user?
Can the tiles be rotated?
Is color the only match or do the symbols have to match as well?
Etc.
Assu$$anonymous$$g these are not drawn by GUI you can check if their material's color are the same with a code something like this:
GameObject letter1;
GameObject letter2;
if(letter1.renderer.shared$$anonymous$$aterial.color == letter2.renderer.shared$$anonymous$$aterial.color)
{
//same color
}
I wrote shared$$anonymous$$aterial because in this case it works and does not make a copy of the material like renderer.material.
Robertbu,
To answer your questions...
2D or 3D? - Game is in 2D
Number of tiles - Number of tiles will be different for each puzzle. Game starts out with 4 tiles and then 9 tiles and so on, as the levels get harder.
Are the tiles placed by the user? - The tiles are not placed by the user. They are stationary but can be flipped vertically and horizontally. To make sure I am understood, the tiles can flip horizontally, or vertically but will always remain at the same coordinates.
Are the tiles moved by the user? - Player can flip the tiles as stated above on$$anonymous$$ouseDown
Can the tiles be rotated? - I will assume you mean clockwise and counterclockwise. No.
Is color the only match or do the symbols have to match as well? - Only the colors have to match
Answer by Kiwasi · Aug 30, 2014 at 10:45 PM
Based on your diagram you only intend to have a limited number of colours. So the best solution would be to forget about colours altogether. Define a property on the GameObject that represents the colour. Then compare the property directly. An enum might be a good idea. You can then use this enum to determine what colour to place on the object.
Answer by robertbu · Aug 31, 2014 at 06:16 AM
While it is possible to raycast and check texture color, typically this kind of problem is solved by building data structures, and checking data. Here is an example script. This script would go on each tile:
using UnityEngine;
using System.Collections;
public class ColorMatch : MonoBehaviour {
public enum Colors {
RED,
GREEN,
BLUE,
YELLOW,
WHITE,
BLACK,
GREY,
PURPLE,
ORANGE,
}
public Colors top;
public Colors right;
public Colors bottom;
public Colors left;
public ColorMatch cmTop;
public ColorMatch cmRight;
public ColorMatch cmBottom;
public ColorMatch cmLeft;
void OnMouseDown() {
Debug.Log ("Here");
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
transform.Rotate (180,0,0);
Colors tmp = top;
top = bottom;
bottom = tmp;
}
else {
transform.Rotate (0,180,0);
Colors tmp = right;
right = left;
left = tmp;
}
CheckColors();
}
void CheckColors() {
if (cmTop != null && cmTop.bottom == top) {
Debug.Log ("Top color matches");
}
if (cmRight != null && cmRight.left == right) {
Debug.Log ("Right color matches");
}
if (cmBottom != null && cmBottom.top == bottom) {
Debug.Log ("Bottom color matches");
}
if (cmLeft != null && cmLeft.right == left) {
Debug.Log ("Left color matches");
}
}
}
After you attach this script to a tile, you will see in the inspector that top, right, bottom, and left are all assigned the color RED. You need to pick the appropriate color for each side of the tile. If you are going to have a bunch of the same tile, it would pay to do a prefab and therefore only have to assign the colors one for each tile. You can edit the Colors enum to only contain the colors used on your tiles.
cmTop, cmRight, cmBottom, and cmLeft are references to the ColorMatch scripts of the adjacent tiles. If there is no adjacent tile, the value is left to 'None' which is null. You can do this by drag and drop. Layout a pattern of tiles. Name the game objects in the hierarchy so you know by their names their position, select a tile, and then drag and drop the appropriate tile into the appropriate slot. For example say you had a layout of four tiles, the top left tile would have a neighbor to the right and a neighbor below. You would select the tile and drag the bottom left tile into the cmBottom slot and the top right tile into the cmRight slot.
All this dragging and dropping can be time consuming and error prone. After you verify the logic by setting up a 2x2 by hand, I'd work on a way to automatically do the layout and assignment of the cmTop, cmRight, cmBottom, and cmLeft variables.
The script just has something simple for the flipping. Shift-click rotate the tile around the horizontal axis, and click rotates it around the vertical. Note how the value of 'top' and 'bottom' are switched when the tile is rotated horizontally, and the swap of 'left' and 'right' for a vertical rotation. This means that top, right, bottom and left will always have the correct "world" values no matter how the tile is flipped.
The CheckColors() function is pretty straight forward. For example, you check to see if there is a tile above you, check to see if cmTop is not null. If it is not, you check this tile's 'top' against the bottom of the tile above.
Whew, it took a lot longer to explain it then it did to write the code. This should get you started.
Hi robertbu, how would you reach the same goal if the player is able to drag the tiles itself into the field? I plan to do something similar, but am a bit stuck there, since there are no fixed positions at all.
I hope you read it and can give me a hint. Btw i do it in 3D but only compare the top of the tile. Like you do in 2d.
Thanks in advance
Your answer

Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Color variable matching 2 Answers
Changing two different objects renderer colour 1 Answer
Game says colors are different but they actually aren't? 1 Answer
Pattern Detection 1 Answer