How can I compare two object by color?

I am new in unity and c#. I want to compare two game objects and show a message. From the picture you can see there is five cubes. I want to match with single one. We see the single cube is red. Now if I click on red cube from the 4 cubes its show “Matched” if I click on black, green or blue cube its show “Not Matched”
85748-cusersnibirdesktop2017-01-12-0154.png

Hint: Code for the 4cubes

public class NewBehaviourScript : MonoBehaviour {

    //  The table where you drag and drop your cubes in the inspector
    [SerializeField]
    GameObject[] gameObjects = new GameObject[4];
    //  The list of the possible colors
    Color[] colors = { Color.green, Color.red, Color.blue, Color.black };

    void SetNewRandomColor()
    {
        int random;
        Color tempColor;

        for (int i = 0; i < 4; i++)
        {
            //  Setting a new color to the GameObject at index i
            random = Random.Range(i, 4);
            gameObjects*.GetComponent<MeshRenderer>().material.color = colors[random];*

// Removing this color from the pickable colors
tempColor = colors*;*
colors = colors[random];
colors[random] = tempColor;
}
}

void Update()
{
// Listening to mouse click
if (Input.GetMouseButtonDown(0))
{
SetNewRandomColor();
}
}

}
Code for Single Cube
public class NewBehaviourScript1 : MonoBehaviour {

public GameObject cube;
public Color[] colors = { Color.green, Color.red, Color.blue, Color.black };

// Use this for initialization
void Start () {

* }*
void SetCubeColor()
{
gameObject.GetComponent().material.color = colors[Random.Range(0, colors.Length)];
}

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
SetCubeColor();
}

}

@milleniu can you help me?

Before answering your question, I would like to give you some tips. Try to give representative name to your class (your code files). Having files named NewBehaviourScript and NewBehaviourScript1 is not logical since you can’t tell that this script is actually doing without opening it and looking at it. Even though it could be simple if you have 2 or 3 scripts, imagine if you had 10, 100 or even more. Another thing is to try to avoid duplicate. For example, both script you wrote are almost similar and almost do the same thing, and by doing this you have twice an Update function which could be simplified in a single one.

To help you have better base for Unity I recommend you watching / reading some of these (non exhaustive list) :

[brackeys youtube channel][1]

[unity3dstudent website][2]

[noobtuts tutorials][3] (not everything is free, but those free one are a really good place to start and give you ideas on how to make little games).


To answer your question :

You can do that by using two scripts. One which will act as a manager, it holds the list of all cubes and do tests and modifications. The other one will be just a simple script on your top four cubes which we allow them to communicate with that game manager, and so, to do your logic tests.


First of all the ActorGameManager :

using UnityEngine;

public class ActorGameManager : MonoBehaviour {
	//  Drag and drop the single cube here
	[SerializeField] GameObject singleCube;
	//  Drag and drop the four cube in this table
    [SerializeField] GameObject[] multipleCubes = new GameObject[4];
	//  The list of color
	Color[] colors = { Color.green, Color.red, Color.blue, Color.black };

	//  SetColor() :
	//  	Change the color of all five cubes
	void SetColor () {
		int random;
		Color tempColor;

		//  Changing the single cube color
		singleCube.GetComponent<Renderer>().material.color = colors[Random.Range(0, colors.Length)];

		//  Changing all cube colors
		for (int i = 0; i < 4; i++) {
			random = Random.Range(i, 4);
			multipleCubes*.GetComponent<MeshRenderer>().material.color = colors[random];*

_ tempColor = colors*;_
_ colors = colors[random];
colors[random] = tempColor;
}
}*_

* // CheckAndUpdateColor :*
* // Check if the given color is the same as the single cube*
* // Then update all colors*
* public void CheckAndUpdateColor (Color color) {*
* // Checking if the color is the same as the single cube*
* if (color == singleCube.GetComponent().material.color) Debug.Log(“Matched”);*
* else Debug.Log(“Not Matched”);*
* // Changing all colors*
* SetColor();*
* }*

* // Start :*
* // Assign a new color to every cube at the start*
* void Start () {*
* SetColor();*
* }*
}
Basically, it’s a fusion of the two scripts you wrote above. You drag and drop both the single cube and the four cube in there, and it will seal the deal. The function SetColor() update both the color of the single cube and the all cubes, and we call it once when we start so your cubes aren’t white.
Note : This script would be better placed on a different object than your cubes, for example an empty, you can create one with the following menu : Game Object => Create Empty.
----------
The ActorCube :
using UnityEngine;

public class ActorCube : MonoBehaviour {

// Drag and drop the item which holds the ActorGameManager script
[SerializeField] GameObject gameManager;

// Local variable for the script and the material of this gameObject
ActorGameManager actorGameManager;
Material material;

// Start :
// Set the local variable at the start
void Start () {
actorGameManager = gameManager.GetComponent();
material = gameObject.GetComponent().material;
}

// OnMouseDown :
// When we click on this object :
// Send this gameObject color to the gameManager
// Then update all colors (see ActorGameManager code)
void OnMouseDown () {
actorGameManager.CheckAndUpdateColor(material.color);
}
}
This script must be placed on all top four cubes. Its job is pretty simple : It listens to click, if it gets a click, then a send the clicked game object’s color to the game manager, then the game object is able to tell if the cube clicked had the same color or not.
The way to do this is by having a public function in your game manager, which is being called by your top cubes when you click on them. You can call this function since you did drag and drop the game object with the manager on every cube. So we can call a public function from a public class present on this game object (the one which have the game object) thanks to the GetComponent() function.
In short, one empty with the manager where you drag and drop all cubes. All top four cubes have a script (ActorCube) on them which allow them to contact this manager. Nothing on the bottom cube.
Feel free to ask me any question what I just wrote if it’s confusing.
*[1]: https://www.youtube.com/user/Brackeys*_
[2]: http://www.unity3dstudent.com/*_
_[3]: https://noobtuts.com/unity*_