0
votes

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!

2
OnMouseDown() has to be properly capitalized to work. You also don't need to check Input.GetMouseButtonDown since it's in OnMouseDown. Also, OnMouseDown requires a GUIElement or Collider; your object seems to have neitherFoggzie
@Foggzie I saw a video on youtube of someone just clicking a 3d gameobject and changing it's material. That's essentially what I want to do with a 2d sprite and its color. I think my issue is that I generated a hex map instead and need to change individual hexes within it but I'm not sure. Here's the video I mentioned: youtube.com/watch?v=6zNHaLdVk80&t=206sJon
Hexprefab is what kind of object ? is that any button? or any UI Elements that can able to take clickes ?Jasil
Foggzie already gave you the answer. Add a 2D collider for starters.Reasurria

2 Answers

1
votes

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.

0
votes

add collider and rigid body to your prefab then capital O in OnMouseDown and delete "if (Input.GetMouseButtonDown(0)){" Input.GetMouseButtonDown(0) return true when player click anywhere which is not what you want

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;
        }


            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