I’m making a 2D game and I’m using 3D cubes, I need to determine which cube from an 8x8 grid is being clicked on, lower it’s hp, and change the colour. This is what I have so far, but it doesn’t work and I’m not sure why.
using UnityEngine;
using System.Collections;
public class colorChange : MonoBehaviour
{
int hp = 3;
// Use this for initialization
void Start()
{
GetComponent<Renderer>().material.color = Color.green;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.name == "Cube")
{
if (hp == 3)
{
this.GetComponent<Renderer>().material.color = Color.yellow;
hp--;
}
else if (hp == 2)
{
this.GetComponent<Renderer>().material.color = Color.red;
hp--;
}
else
{
this.GetComponent<Renderer>().material.color = Color.black;
hp--;
}
}
}
}
}
}