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
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!
Input.GetMouseButtonDown
since it's inOnMouseDown
. Also,OnMouseDown
requires a GUIElement or Collider; your object seems to have neither – Foggzie