Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Highlighting Tiles

Discussion in 'Scripting' started by rapidcatch4464, Jan 25, 2020.

  1. rapidcatch4464

    rapidcatch4464

    Joined:
    Jan 20, 2020
    Posts:
    9
    Through scripting, I'm changing the color of the tile Image whenever OnMouseDown() is triggered for a tile. It's going to represent where the player is headed. However, if the character stops on the tile OR changes to go to another tile, then the color should go back to normal.

    I'm stuck at the "disabling color" part. How can I tell the tile that it needs to turn the color off if another tile is selected, or the player has stopped moving? All the tiles are from one prefab, so I'm confused how I can communicate between them...

    Code (CSharp):
    1. public class tileScript : MonoBehaviour
    2. {
    3.     [SerializeField] playerScript playerScript;
    4.     public bool toolOccupied;
    5.     public bool playerOccupied;
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         TileColor();
    11.     }
    12.  
    13.     public void TileColor()
    14.     {
    15.         if (playerOccupied && !playerScript.moving)
    16.         {
    17.             GetComponent<Image>().color = new Color32(0, 0, 0, 0);
    18.         }
    19.     }
    20.  
    21.     private void OnMouseOver()
    22.     {
    23.         if(Input.GetMouseButtonDown(1) && !playerOccupied && !toolOccupied)
    24.         {
    25.             playerScript.tileX = transform.position.x;
    26.             playerScript.tileY = transform.position.y;
    27.             GetComponent<Image>().color = new Color32(0, 255, 0, 225);
    28.         }
    29.     }
    30.  
    31.     private void OnTriggerEnter2D(Collider2D collision)
    32.     {
    33.         if (collision.gameObject.tag == "Player")
    34.         {
    35.             playerOccupied = true;
    36.         }
    37.  
    38.         if (collision.gameObject.tag == "Tool")
    39.         {
    40.             toolOccupied = true;
    41.         }
    42.  
    43.     }
    44.  
    45.     private void OnTriggerExit2D(Collider2D collision)
    46.     {
    47.         if (collision.gameObject.tag == "Player")
    48.         {
    49.             playerOccupied = false;
    50.         }
    51.     }
    52.  
    53. }
     
  2. rapidcatch4464

    rapidcatch4464

    Joined:
    Jan 20, 2020
    Posts:
    9
  3. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    What I do for my UI is put transluscent planes over icons and show/hide them as I see fit. It looks just like highlighting. I don't know how your tile system is visually, but perhaps you could do something similar.

    Since each tile is presumably linked to the same material, which is in turn linked to the same image, changing the image will change them all. Another approach you could take would be to duplicate the image in your folder, change that one permanently, and assign it to a new material. Then, you could just switch the material at runtime when moused-over or clicked or whatever.
     
  4. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    You don't mean tiles as in tiles from a tilemap do you?

    Store the position of the last tile on OnMouseOver, and when you select a new tile, revert the color of the stored position.
     
    rapidcatch4464 likes this.
  5. rapidcatch4464

    rapidcatch4464

    Joined:
    Jan 20, 2020
    Posts:
    9
    Thank you! I added a Vector2 to store the destination position for the character. Then added an if statement to the tile, saying if the destination.transform was equal to the tile.transform, then it should highlight. Otherwise, return to normal color.