This is a question I recently posted that has been answered. But it in this instance I have multiple Blocks in my scene

with the same scripts attached to them
public GameObject otherobj;
private float blueMultiply = 3.50f;
private float redGreenMultiply = 0.50f;
private Color originalColor;
private void Start()
{
originalColor = gameObject.GetComponent<Renderer> ().material.color;
}
void OnMouseDown()
{
AddHighlight();
RemoveHighlight();
}
private void AddHighlight()
{
float red = originalColor.r * redGreenMultiply;
float blue = originalColor.b * blueMultiply;
float green = originalColor.g * redGreenMultiply;
gameObject.GetComponent<Renderer> ().material.color = new Color(red, green, blue);
}
private void RemoveHighlight ()
{
otherobj.GetComponent<Renderer> ().material.color = originalColor;
}
How can I bulk up my script so that I can anytime I Click a block it is highlighted. And when I click another block the original block Highlight is removed. In this example I have 8 blocks, in another example I may have more. How can I remove the highlight when any other game object is clicked
Change AddHighlight to something like this:
private void AddHighlight ()
{
ThisScriptsName[] allBlocks = Object.FindObjectsOfType <ThisScriptsName> ().gameObject;
foreach (ThisScriptsName o in allBlocks) {
o.GetComponent<Renderer> ().material.color = originalColor;
}
float red = originalColor.r * redGreenMultiply;
float blue = originalColor.b * blueMultiply;
float green = originalColor.g * redGreenMultiply;
gameObject.GetComponent<Renderer> ().material.color = new Color(red, green, blue);
}
Using @ThisTestWillDo answer as well as this reference, here is my working updated script:
private float blueMultiply = 3.50f;
private float redGreenMultiply = 0.50f;
private Color originalColor;
private void Start()
{
originalColor = gameObject.GetComponent<Renderer> ().material.color;
}
void OnMouseDown()
{
AddHighlight();
}
private void AddHighlight()
{
ThisScriptsName[] allBlocks = FindObjectsOfType (typeof(ThisScriptsName)) as ThisScriptsName[];
foreach (ThisScriptsName o in allBlocks) {
o.GetComponent<Renderer> ().material.color = originalColor;
}
float red = originalColor.r * redGreenMultiply;
float blue = originalColor.b * blueMultiply;
float green = originalColor.g * redGreenMultiply;
gameObject.GetComponent<Renderer> ().material.color = new Color(red, green, blue);
}
}