- Home /
Keep gameobject highlighted until another gameobject is selected (multiple gameobjects)
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
Answer by ThisTestWillDo · Apr 29, 2016 at 06:29 AM
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);
}
Thanks for the response. When I use your code:
ObjectHighlight[] allBlocks = Object.FindObjectsOfType ().gameObject;
foreach (ObjectHighlight o in allBlocks) {
o.GetComponent<Renderer> ().material.color = originalColor;
I receive a syntax error 'System.Array does not contain a definition for 'gameObject'
I brushed it up and got it to work!! Thank you so much! I will post my answer below yours for future reference.
Answer by birns92 · Apr 29, 2016 at 07:32 AM
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);
}
}
Your answer

Follow this Question
Related Questions
How to prevent a script on a disabled object from firing? 0 Answers
How to access a game object from a different scene? 0 Answers
Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers