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

board of sprites,click on sprite and use its identity

Discussion in '2D' started by kantescore, Jun 14, 2019.

  1. kantescore

    kantescore

    Joined:
    Dec 4, 2018
    Posts:
    7
    i would like to know how can i implement the same script for 39 game square sprites of monopoly so that
    each one ,when clicked,will run piece of code that will show "this.name" of the sprite
    i need this so that i can check in the Board class that contains array of Tile class the information of the specific game square.

    thank you,
     
  2. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    21
    You are not implementing sprites. You are implementing buttons, that you set the sprite of. If you want to do that with a script "New Button", set the "sprite" property, then add a "Listener" object to the button.

    If you are new, I would just add the UI object in the inspector, set the sprite, then add a listener to a function that uses Debug.Log. Then copy and paste it 39 times.
     
  3. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    55
    I would rather implement a script like that:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tile
    6. {
    7.     private bool mouseOver;
    8.     private void OnMouseOver()
    9.     {
    10.         //Display information of the tile
    11.     }
    12.     private void OnMouseExit()
    13.     {
    14.         // Hide Information of the Tile
    15.     }
    16.  
    17.     private void Update() {
    18.         if(mouseOver && Input.GetMouseButtonDown(0))
    19.         {
    20.             // Do the action you want
    21.         }
    22.     }
    23. }