Referencing across scipts without game object

First Script

 public float BustChance()
    {
        
        foreach (int i in cards)
        {
            int cardValue = i % 13;
            if (cardValue > 21 - player.HandValue())
            {
                counter++;
            }

        }
        float odds = counter / cards.Count;
        return odds;
    }

Second Script:

    private void Update()
    {
        probabilityValue.text = BustChance().ToString();
    }

Hi,

How can I reference BustChance() in the second script as it is not connected to any game object? For context, I am trying to work out the probability of the user going bust if they were to hit in a round of Blackjack, and display that value to the user on the screen (Hence the second section of code).

Thanks

Create an empty gameobject (name it something like Logic) and assign the first script to it.

That should make it available.

Also make it public static float bustChance() as you only want that one instance of the method.

Create two empty GameObjects. One called Player, one Logic.

Assign these two scripts:

Player.cs

using UnityEngine;
using System.Collections.Generic;

public class Player : MonoBehaviour
{
    private float probabilityValue;
    public static List<int> cards;

    private void Start()
    {
        cards = new List<int>();
        cards.Add(1);
        cards.Add(2);
        cards.Add(9);
        cards.Add(10);
    }

    private void Update()
    {
        probabilityValue = Logic.BustChance();
        Debug.Log("Probability="+probabilityValue);
        Debug.Log("Hand value="+HandValue());

    }

    public static int HandValue()
    {
        int hv = 0;
        foreach (int i in cards)
        {
            hv += i;
        }
        return hv;
    }
}

Logic.cs

using UnityEngine;

public class Logic : MonoBehaviour
{
    private static int counter = 0;
    private static int odds = 0;

    public static float BustChance()
    {
        counter = 0;
        foreach (int i in Player.cards)
        {
            int cardValue = i % 13;
            if (cardValue > 21 - Player.HandValue())
            {
                counter++;
            }

        }
        float odds = counter / Player.cards.Count;
        return odds;
    }
}