# Changing color of only one gameobject

So i have this script the checks if my mouse cursor is over the block and if it is i want the block to change color

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicBlockFunction : MonoBehaviour {

public GameObject Block;
public Color StartingColor;
public Color HighlightColor;
Color color;
Ray ray;
RaycastHit hit;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
MouseOver();
}

void MouseOver()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit))
{
print(hit.collider.name);
Block.GetComponent<Renderer>().material.color = HighlightColor;
}
else
{
Block.GetComponent<Renderer>().material.color = StartingColor;
}

}

}


I want to change the color only for the gameobject i have my mouse on.

• it work but changes the color on every gameobjects with this script. – HearPcGaming Mar 8 '17 at 17:00
• i want it to change only the color of the block i have my mouse cursor on. – HearPcGaming Mar 8 '17 at 17:01
• You could do it based off of an if(hit.gameObject.tag == "object") { // Do stuff. } – n_plum Mar 8 '17 at 17:02
• Here seems to be some confusion here. What is this script attached to? It should be the camera not the block. And any object with a collider can be hit by a raycast – Mr.Bigglesworth Mar 8 '17 at 17:13
• Yes so at the moment you are referencing your Block type but you want to refer to the hit variable in the raycast. This will be the block you are pointing at – Mr.Bigglesworth Mar 8 '17 at 17:57

If your block objects each have their own colliders, you can do this with a script you attach to each block, leveraging Unity's built-in OnMouseEnter / OnMouseExit events:

[RequireComponent(typeof(Renderer))]
public class HoverHighlight : MonoBehaviour {

public Material highlightMaterial;

Renderer _renderer;
Material _originalMaterial;

void Start() {
_renderer = GetComponent<Renderer>();
_originalMaterial = _renderer.sharedMaterial;
}

void OnMouseEnter() {
_renderer.sharedMaterial = highlightMaterial;
}

void OnMouseExit() {
_renderer.sharedMaterial = _originalMaterial;
}

}


Instead of changing the material color, you could change the material.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicBlockFunction : MonoBehaviour {

public GameObject Block;
public Material StartingMaterial;
public Material HighlightMaterial;
Color color;
Ray ray;
RaycastHit hit;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
MouseOver();
}

void MouseOver()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit))
{
print(hit.collider.name);
Block.GetComponent<Renderer>().sharedMaterial = HighlightMaterial;
}
else
{
Block.GetComponent<Renderer>().sharedMaterial = StartingMaterial;
}

}

}


This would give you more control over the highlight material.

• doesn't work :( – HearPcGaming Mar 8 '17 at 17:11
• @HearPcGaming whenever you're giving feedback on an answer, or trying to describe a problem, please ensure you always give more details than "doesn't work." You should describe the exact symptoms you're seeing (eg. error messages, unexpected behaviour, absence of desired behaviour) - this gives other users better guidance about how to help you and improve their answers. Here I infer that, because this script doesn't change how it's selecting which block to change, it has the same issue as your original version: multiple copies of the script all run, changing multiple blocks instead of just one – DMGregory Mar 9 '17 at 2:06
• It has the same issue as my original version. – HearPcGaming Mar 9 '17 at 6:01

You are referring to a Block game object not the object being hit by the array. To change the object the raycast hits use the hit variable. This stores all info about the object including its material and colour.