Search code examples
c#unity-game-enginespritehexagonal-tiles

How to Change Color of Game Object When Clicked in Unity?


I'm trying to create the board game Hex. Player One at the bottom being yellow and Player Two at the top being blue. When Player One clicks a hex it should become yellow and when Player two clicks a hex it should become blue.

I've created this Hex Map using a prefab and now I want to be able to change the color of each tile when I click on it (The yellow hexes you see will be transparent but the sprite I imported is yellow which is why the color on the Sprite Renderer is white even though the hexes look yellow).

Btw, as of right now changing the color in the Sprite Renderer changes the color of all the hexes.

I followed quill18creates's tutorial to make the Hex Map except I did it in 2D instead of 3D.

https://www.youtube.com/watch?v=j-rCuN7uMR8

enter image description here

As of write now my Color Change script isn't working at all. I was trying to have it so when it receives one click it changes to yellow. Then the next click to blue, the next to yellow, and so on. Since each player only get's one click.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorChange : MonoBehaviour {

    public Color[]colors; // allows input of material colors in a set sized array
    public SpriteRenderer rend;  // what are we rendering? the hex

    private int index = 1; //initialize at 1, otherwise you have to press the ball twice to change color


    // Use this for initialization
    void Start () {
        rend = GetComponent<SpriteRenderer> (); // gives functionality for the renderer
    }

    // Update is called once per frame
    void onMouseDown () {
        // if there are no colors present nothing happens
        if (colors.Length == 0){
            return;
        }

       if (Input.GetMouseButtonDown(0)){
           index += 1; // when mouse is pressed down we increment up to the next index location

           // when it reaches the end of the colors it stars over
           if (index == colors.Length +1){
               index = 1;
           }

        print (index); // used for debugging
        rend.color = colors [index - 1]; // this sets the material color values inside the index
       }
   } //onMouseDown
}

How should I go about implementing this? Any help would be much appreciated!


Solution

  • First things first, you need to properly capitalize OnMouseDown() for it to get called.

    After that, since you're using a SpriteRenderer, you'll need to add a collider to detect mouse click events. As mentioned in the OnMouseDown() documentation:

    This event is sent to all scripts of the Collider or GUIElement.

    Since you have neither, on your prefab, click Add Component > Polygon Collider 2D and it'll automatically create the proper geometry for your sprite (assuming that everything outside the hex is transparent).

    Finally, remove your check against Input.GetMouseButtonDown(0). OnMouseDown is already capturing the fact that the mouse was clicked and the specific instance running OnMouseDown() was the instance that was clicked.