Search Unity

Making Element in List Clickable

Discussion in '2D' started by wraith1821, May 22, 2018.

  1. wraith1821

    wraith1821

    Joined:
    Nov 26, 2017
    Posts:
    30
    I'm creating a board game.

    It has 81 Squares, and I've created 81 empty gameobjects with colliders (triggers).

    The squares live in a List show in the image in the inspector and each has a value of 0 through 4. The color enum is pointless at the moment. I think.

    I'm trying to click on the square and have it return in the log that it was clicked and what the value of the square is.

    Later Ill be validating the moves by checking the value of the square the player clicked on their previous turn, and instantiating a game piece if valid, but I just want to make the clicks work at this point. I've hunted exhaustively and can't find how to do this with a List. I can get it to return the value with a script attached to the square prefab. Maybe I'm doing the whole thing wrong. I feel as though I should be able to assign the attributes of each square in the List within the gamecontroller script without having to use a script attached to the square prefab. The board itself is just a sprite.

    The Debug.Log in start is there because i just wanted anything to happen. lol

    Code is below along with image of Board.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour {
    6.  
    7.     public square squareScript;
    8.  
    9.  
    10.     public GameObject playerXGamePiece;
    11.     public GameObject playerOGamePiece;
    12.  
    13.     public int PlayerX = 0;
    14.     public int PlayerO = 1;
    15.  
    16.     public bool isPlayerXTurn;
    17.     public bool isPlayerOTurn;
    18.  
    19.     public enum Squares
    20.     {
    21.         Yellow = 0,
    22.         White = 1,
    23.         Green = 2,
    24.         Blue = 3,
    25.         Magenta = 4
    26.     }
    27.  
    28.     [System.Serializable]
    29.     public class Squares1
    30.     {
    31.         public GameObject playAreaSquare;
    32.         public int squareValue;
    33.         public Squares squareColor;
    34.     }
    35.  
    36.     public List<Squares1> _squares;
    37.  
    38.  
    39.     // Use this for initialization
    40.     void Start () {
    41.        
    42.     Debug.Log("I see " + _squares[0].playAreaSquare + " with a value of " + _squares[0].squareValue.ToString() + " whose color is " + _squares[0].squareColor);
    43.  
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update () {
    48.  
    49.         if (Input.GetMouseButtonDown(0))
    50.         {
    51.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    52.             RaycastHit hit;
    53.  
    54.             if (Physics.Raycast(ray, out hit, 100))
    55.             {
    56.                 Debug.Log(hit.transform.gameObject.name);
    57.             }
    58.         }
    59.  
    60.     }
    61.     private void OnMouseDown()
    62.     {
    63.  
    64.     }
    65.  
    66.  
    67. }
     

    Attached Files: