Question by
wang3922 · Aug 22, 2015 at 10:37 AM ·
mousegridcloneshighlighting
Highlighting all clones instead of the one the mouse is over.
I have an empty GameObject called IGController with a script that instantiates a prefab into a 9x9 grid. On the prefab is another script that is supposed to highlight one grid object when moused over. Instead it highlights all of them. How can I get it to only highlight the one my mouse is over?
I know there are plenty of Unity questions like this, but I couldn't seem to get it working even after reading them..
---------- Script on IGController: ----------
using UnityEngine;
using System.Collections;
public class IGController : MonoBehaviour {
public GameObject trackPiece;
public int gridSpacing = 10;
public int gridWidth = 9;
public int gridHeight = 9;
void Awake () {
GameObject [,] grid = new GameObject [gridWidth, gridHeight];
for (int x = 0; x < gridWidth; x++) {
for (int z = 0; z < gridHeight; z++) {
GameObject gridPlane = (GameObject) Instantiate (trackPiece);
gridPlane.transform.position = new Vector3(gridPlane.transform.position.x + (x * gridSpacing), gridPlane.transform.position.y, gridPlane.transform.position.z + (z * gridSpacing));
grid [x,z] = gridPlane;
}
}
}
}
---------- Script on prefab: ----------
using UnityEngine;
using System.Collections;
public class Highlight : MonoBehaviour {
public Renderer rend;
public Material normalColor;
public Color highlightColor;
void Start () {
rend = this.GetComponent<Renderer>();
rend.material = normalColor;
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, Mathf.Infinity)) {
rend.material.color = highlightColor;
}
else {
rend.material = normalColor;
}
}
}
Comment