Question about puzzle game checking for other objects

What would be the most efficient way of checking what happens on the picture? when 2 of the same colors touch? what are some good ways and what would you recommend using for this.
the molecules can rotate 6 times.

I was thinking of using a raycast but I’m very affraid for my performance if I’d use that.

alt text

EDIT: this is also displayed in one of my replies to the topic but this will clarify what I mean and what situation I’m in.

Yes, the game is grid based there will be a grid full of these molecules and if lets say for example you have 3 molecules, you have the 2 I displayed on the picture and there will be an other one that connects with red to red on the other molecule that way there will be 3 molecules touching eachother in a line example but keep in mind that there can also be 4 or 5 lined up:
alt text

The grid will contain an amount of molecules so they are clearly visible and easily clickable on any regular size mobile phone, 30-40 would be an amount, considering the fact that some of the molecules can be surrounded by other ones I would have to cast 3 raycasts from every single molecule that way from each color.
Each molecule is built out of primitives, one sphere and 3 colored cilinders, nothing fancy there won’t be anything fancy either. I was first planning to do the touching with raycasts so I wrote code that would support that easily, how it’s done now is when you click on one of the molecules it will up the status and then it will change the rotation of the object to the rotation that belongs to the status.

Here you can see what I mean:

	public int status;
void Start(){
		status = Random.Range(0, 6);
	}
void Update(){
switch(status){
		case 0:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 300);
			break;
		case 1:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 240);
			break;
		case 2:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 180);
			break;
		case 3:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 120);
			break;
		case 4:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 60);
			break;
		case 5:
			gameObject.transform.eulerAngles = new Vector3(
				0, 0, 0);
			break;
		}

			}
	public void StatusUpdate(){
		if(status == 5)
			status = 0;
		else 
			status++;
	}

I hope I have given enough information this way, thanks in advance!

@NoseKills Could you give me an example of this as I don’t really seem to understand what you mean? currently I create a grid with molecules and they spawn a certain way so the edges touch eachother.

How can I diagonally cast a raycast?

You could use colliders and Kinematic rigidbodies with constraints.

Colour checking can be performed in OnCollision functions.