Search Unity

Question Explaining how this single file allows game to determine what is clicked from variety of objects

Discussion in '2D' started by Polypana, Jul 27, 2020.

  1. Polypana

    Polypana

    Joined:
    Jun 22, 2020
    Posts:
    32
    Hi again!

    As I'm sure you know, I'm working on a scissors-paper-rock game. I've been given a version with working code created by someone else to help me, but I don't want to copy their code without understanding it decently first. I previously had created 3 different files with different classes to determine whether or not the player clicked on scissors, paper or rock.

    They have given me a single file that apparently handles all of this:

    Code (CSharp):
    1. using ScissorsPaperRock;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class SelectObject : MonoBehaviour, IPointerClickHandler
    8. {
    9.     public UNIT Choice;
    10.     // Start is called before the first frame update
    11.     public void OnPointerClick(PointerEventData pointerEventData)
    12.     {
    13.         GameObject.Find("GameManager").GetComponent<GameHandler>().ProcessPlayerChoice(Choice);
    14.     }
    15. }
    16.  
    It is attached to all 3 objects. There's an event system. When I look at the attached script it has a drop down menu that allows me to select from one of the 3 enums. It seems all good.

    But what I don't understand is quite how it works. Hence why I'm asking for a relatively simple (if possible) explanation. I don't want to copy their code unless I've made an effort to understand what it does. So could you please explain to me how the code works? I figure it finds what object I clicked on and determines the selection based on that, but I don't quite understand how.

    Thanks!