- Home /
Question by
gokseninbarutcu · Mar 08, 2017 at 04:04 PM ·
c#detectclick objects
Can I change the color of image when clicked?
Hello Everyone,
I have a 2x2 game table having 4 balls in it. When I press any of them, I want them to change its color to grey. Currently both table and balls are just images I painted in Paint and they are all white with black borders.
I tried Raycast and OnMouseDown but none of them helped. Here it is the lastest version of my script. If you help me, I would appriciate a lot.
public class BallColor : MonoBehaviour {
//class variables;
bool isRotating;
GameObject gameTable;
private void Start()
{
isRotating = false;
}
void Update()
{
if (!isRotating) {
print("dönmüyor");
isRotating = true;
print("dönmeye başladı");
var ray = Camera.main.ViewportPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
var hit = Physics2D.GetRayIntersection(ray);
print("hit: " + hit);
if (hit.collider != null)
{
hit.transform.GetComponent<Renderer>().material.color = Color.grey;
gameTable = GameObject.FindGameObjectWithTag("PlayTable");
gameTable.transform.Rotate(0, 0, -90);
}
isRotating = false;
Comment
Answer by javoastudillo · Mar 15, 2017 at 09:14 PM
I imagine the ball has a collider attached, so I would try
void OnMouseDown () { this.gameObject.GetComponent<SpriteRenderer>().color = Color.grey; }
I don't see why OnMouseDown()
would fail.