Hey all,
I’m currently working on a grid system. Right now I’ve got my grid being generated, and each tile will change colour if it is mouse overed. My question is, how can I achieve this effect without using Rigidbodies on my instantiated tiles? I don’t want my characters, who will be walking on/in these tile objects to kick them about.
Secondary question, the tiles change to a different colour if they are clicked on. How can I change my code so that they stay that different colour once the collision has ended? Thanks!
Code to generate grid:
using UnityEngine;
using System.Collections;
public class Grid : MonoBehaviour {
public int m_Units;
private GameObject tempUnitTile;
private int i;
private int columns;
private int rows;
public Camera m_Camera;
ArrayList GridArray;
private void Awake()
{
GridArray = new ArrayList();
for (i = 0; i < (m_Units * m_Units); i++)
{
tempUnitTile = GameObject.Instantiate(Resources.Load("UnitTile"), new Vector3(columns + 0, 0, rows + 0), Quaternion.identity) as GameObject;
tempUnitTile.name = "Unit Tile" + (i + 1);
GridArray.Add(tempUnitTile);
columns++;
if (columns == m_Units)
{
columns = 0;
rows++;
}
}
}
private void Update()
{
RaycastHit hit;
string colliderName;
GameObject m_TileHit;
if (!Physics.Raycast(m_Camera.ScreenPointToRay(Input.mousePosition), out hit, 1000f))
return;
colliderName = hit.collider.name;
m_TileHit = hit.collider.gameObject;
Debug.Log(colliderName);
if (Input.GetMouseButton(0) == true)
{
m_TileHit.renderer.material.color = new Color(0f, 0f, 1f, 0.3f);
}
else
{
m_TileHit.renderer.material.color = new Color(1f, 0f, 0f, 0.3f);
}
}
}
Code on the tile prefab to reset the colour once collision has ended:
using UnityEngine;
using System.Collections;
public class ColorReset : MonoBehaviour {
private void Update()
{
this.gameObject.renderer.material.color = new Color(0.45f, 1f, 0.55f, 0.3f);
}
}
Thanks! ![]()