0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ it work but changes the color on every gameobjects with this script. \$\endgroup\$ – HearPcGaming Mar 8 '17 at 17:00
  • \$\begingroup\$ i want it to change only the color of the block i have my mouse cursor on. \$\endgroup\$ – HearPcGaming Mar 8 '17 at 17:01
  • \$\begingroup\$ You could do it based off of an if(hit.gameObject.tag == "object") { // Do stuff. } \$\endgroup\$ – n_plum Mar 8 '17 at 17:02
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Mr.Bigglesworth Mar 8 '17 at 17:13
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Mr.Bigglesworth Mar 8 '17 at 17:57
3
\$\begingroup\$

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;
    }

}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ doesn't work :( \$\endgroup\$ – HearPcGaming Mar 8 '17 at 17:11
  • \$\begingroup\$ @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 \$\endgroup\$ – DMGregory Mar 9 '17 at 2:06
  • \$\begingroup\$ It has the same issue as my original version. \$\endgroup\$ – HearPcGaming Mar 9 '17 at 6:01
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.