Scoring system help

Hi, i’m working on a project which involves a Rock, Paper, Scissors game within it. I have the game aspect sorted but I am struggling with the scoring system of the game.

So far I have 3 different public voids for each option

    public void OnRockPointerClick()
    {
        randomInt = (Random.Range(1, 4));
        tm = (TextMesh)GameObject.Find("Score").GetComponent<TextMesh>();
        tm.text = "";
       
        if (randomInt ==(1))
        {
            Debug.Log("Draw");
            tm = (TextMesh)GameObject.Find("decision").GetComponent<TextMesh>();
            tm.text = "DRAW"; 
        }
        if (randomInt ==(2))
        {
            Debug.Log("Lose");
            CPUscore += 1;
            tm = (TextMesh)GameObject.Find("decision").GetComponent<TextMesh>();
            tm.text = "CPU WIN"; 
            tm = (TextMesh)GameObject.Find("cpupoint").GetComponent<TextMesh>();
            tm.text += "X "; 
        }
        if (randomInt == (3))
        {
            Debug.Log("Win");
            PlayerScore += 1;
            tm = (TextMesh)GameObject.Find("decision").GetComponent<TextMesh>();
            tm.text = "PLAYER WIN"; 
            tm = (TextMesh)GameObject.Find("playerpoint").GetComponent<TextMesh>();
            tm.text += "X "; 
        }

I know this method probably isn’t the best way to do it but it’s what i’m going with, i’m not too worried about cleaning up my code at the moment as well.

My code for when either the player or computer wins is,

 void Update()
    {
   
      
        if (PlayerScore == 3)
        {
            Debug.Log("You win!!");
            tm = (TextMesh)GameObject.Find("Score").GetComponent<TextMesh>();
            tm.text = "YOU WIN"; 
            tm = (TextMesh)GameObject.Find("playerpoint").GetComponent<TextMesh>();
            tm.text = ""; 
            tm = (TextMesh)GameObject.Find("cpupoint").GetComponent<TextMesh>();
            tm.text = "";
            tm = (TextMesh)GameObject.Find("decision").GetComponent<TextMesh>();
            tm.text = "";
            CPUscore.Equals(0);
            PlayerScore.Equals(0);

        }
        if (CPUscore == 3)
        {
            Debug.Log("You lose!!");
            tm = (TextMesh)GameObject.Find("Score").GetComponent<TextMesh>();
            tm.text = "YOU LOSE";
            tm = (TextMesh)GameObject.Find("playerpoint").GetComponent<TextMesh>();
            tm.text = ""; 
            tm = (TextMesh)GameObject.Find("cpupoint").GetComponent<TextMesh>();
            tm.text = "";
            tm = (TextMesh)GameObject.Find("decision").GetComponent<TextMesh>();
            tm.text = "";
            CPUscore.Equals(0);
            PlayerScore.Equals(0);
        }

    }

I’m having the problem though that (also sorry if this does not make much sense) when a player reaches 3 points it only counts for that button click, its not an overall counter.

So, if a player chooses Rock twice and wins then Scissors once and also wins, the game should count this as having 3 points and the player should win the game. However currently it is Rock 2 points, Scissors 1 point, which doesn’t activate the win sequence.

Any help with this would be great thanks!

I refactored your script to be more cleaner, shorter, more readable and more performant.

https://pastebin.com/48jARq7i

You just map the HandlePointerClick on all the buttons and it should work correctly.

Similar to the other answer (I worked on mine while there was still none :)), here’s my working refactor of your code. Good luck!


using UnityEngine;
using System;
using System.Collections.Generic;
using TMPro;
    
public class GameHandling : MonoBehaviour
{
    // Assign these in inspector.
    [SerializeField] TextMeshPro decisionText = null;
    [SerializeField] TextMeshPro playerScoreText = null;
    [SerializeField] TextMeshPro cpuScoreText = null;

    const int winScore = 3;          
    int playerScore = 0;
    int cpuScore = 0;

    public enum HandType
    {
        Rock = 0,
        Paper = 1,
        Scissors = 2
    }

    Dictionary<HandType,HandType> beats = new Dictionary<HandType,HandType>() {
        {HandType.Rock, HandType.Scissors},
        {HandType.Paper, HandType.Rock},
        {HandType.Scissors, HandType.Paper}
    };
       
    void Update()
    {
        if (Input.GetKeyDown("1")) { OnPointerClick(HandType.Rock); }
        else if (Input.GetKeyDown("2")) { OnPointerClick(HandType.Paper); }
        else if (Input.GetKeyDown("3")) { OnPointerClick(HandType.Scissors); }
    }

    public void OnRockPointerClick() { OnPointerClick(HandType.Rock); }
    public void OnPaperPointerClick() { OnPointerClick(HandType.Paper); }
    public void OnScissorsPointerClick() { OnPointerClick(HandType.Scissors); }

    void OnPointerClick(HandType playerHandType)
    {
        int cpuHandTypeNumber = UnityEngine.Random.Range(
            0, Enum.GetNames(typeof(HandType)).Length);
        HandType cpuHandType = (HandType) cpuHandTypeNumber;
    
        bool? playerWins = playerHandType != cpuHandType ?
            (bool?) (beats[playerHandType] == cpuHandType) : null;
    
        if (playerWins == true)
        {
            decisionText.text = "Player wins round";
            playerScore++;
        }
        else if (playerWins == false)
        {
            decisionText.text = "CPU wins round";
            cpuScore++;
        }
        else
        {
            decisionText.text = "This round is a draw";
        }
    
        print("You use " + playerHandType.ToString() + ". " +
            "CPU uses " + cpuHandType.ToString() + ".");
    
        FinalizeRound();
    }
     
    void FinalizeRound()
    {
        playerScoreText.text = playerScore.ToString();
        cpuScoreText.text = cpuScore.ToString();

        if (playerScore >= winScore)
        {
            decisionText.text = "You win the game!";
            Reset();
        }
        else if (cpuScore >= winScore)
        {
            decisionText.text = "CPU wins the game!";
            Reset();
        }
    }
    
    void Reset()
    {
        playerScore = 0;
        cpuScore = 0;
    }
}