'How do I get send the int value of ScoreNum to another script?

I have this script to calculate the ScoreNum and I want to send the int value into another script. How should I connect the two?

public class QuizManager : MonoBehaviour
{

    public GameObject Quiz1;
    public RadialBar Radial;
    int currentLevel; //to set how many levels are there


    public GameObject[] Levels;//array for the levels
    public float ScoreNum;

    public bool isSequence = true;

    void Start()
    {
        ScoreNum = 0;

        Quiz1.SetActive(true);
    }

  

    public void correctAnswer()
    {
        
        

            Levels[currentLevel].SetActive(false);//if the answer is wrong the question pop up will disable

            currentLevel++;
            Radial.Add(1);


            //if (isSequence)
           {
               ScoreNum += 1.0f;

               
           }
        //else if (!isSequence)
        {
            //ScoreNum += 0.5f;

        }


            Levels[currentLevel].SetActive(true);
        
    }

    public void wrongAnswer()
    {


        Levels[currentLevel].SetActive(false);//if the answer is wrong the question pop up will disable

        currentLevel++;
        Radial.Add(1);

        Levels[currentLevel].SetActive(true);
    }


}

I would like to then send the value into another script:

public class Results : MonoBehaviour
{

    public GameObject ResultsStuff;
    public GameObject scoreText;
    public GameObject starOne;
    public GameObject starTwo;
    public GameObject starThree;
    public GameObject starOneEmpty;
    public GameObject starTwoEmpty;
    public GameObject starThreeEmpty;
    public float ScoreNum;

    //Audio

    public AudioSource audioSuccess;
    public AudioClip success;

    public AudioSource audioNt;
    public AudioClip nt;

    public AudioSource audioFail;
    public AudioClip fail;

    public AudioSource audioFilledappear;
    public AudioClip filledappear;

    public AudioSource audioEmptyappear;
    public AudioClip emptyappear;

    public float volume = 0.5f;

    // Start is called before the first frame update
    void Awake()
    {
        StartCoroutine("Stars");
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public IEnumerator Stars()
    {
        ResultsStuff.SetActive(true);
        scoreText.GetComponent<TextMeshProUGUI>().text = ScoreNum + "/28";

        if (ScoreNum == 0)
        {
            audioFail.PlayOneShot(fail, volume);
            starOne.SetActive(false);
            starTwo.SetActive(false);
            starThree.SetActive(false);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starOneEmpty.SetActive(true);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starTwoEmpty.SetActive(true);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starThreeEmpty.SetActive(true);
        }

        else if (ScoreNum < 10)
        {
            audioNt.PlayOneShot(nt, volume);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starOne.SetActive(true);
            starTwo.SetActive(false);
            starThree.SetActive(false);
            starOneEmpty.SetActive(false);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starTwoEmpty.SetActive(true);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starThreeEmpty.SetActive(true);
        }

        else if ((ScoreNum > 10) && (ScoreNum < 28))
        {
            audioNt.PlayOneShot(nt, volume);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starOne.SetActive(true);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starTwo.SetActive(true);
            starThree.SetActive(false);
            starOneEmpty.SetActive(false);
            starTwoEmpty.SetActive(false);
            yield return new WaitForSeconds(1);
            audioEmptyappear.PlayOneShot(emptyappear, volume);
            starThreeEmpty.SetActive(true);
        }

        else if (ScoreNum == 28)
        {
            audioSuccess.PlayOneShot(success, volume);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starOne.SetActive(true);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starTwo.SetActive(true);
            yield return new WaitForSeconds(1);
            audioFilledappear.PlayOneShot(filledappear, volume);
            starThree.SetActive(true);
            starOneEmpty.SetActive(false);
            starTwoEmpty.SetActive(false);
            starThreeEmpty.SetActive(false);
        }
    }
}

Technically now, the results will always show 0 since the value of the integer has not been set on the second script. I'd be great if anyone could help! Thanks.



Solution 1:[1]

What you were doing was trying to use two different variables as one. There are two ways to achieve what you need to do:

Method 1: (using only C#)

Defining the public float ScoreNum in QuizManager as static:

public class QuizManager : MonoBehaviour
    ....
    public static float ScoreNum;
    ....

Since you defined this variable as static, you can reach it from anywhere.

public class Results : MonoBehaviour
    ...
    // public float ScoreNum; //Delete this
    ...

    public IEnumerator Stars()
        ...
        if (QuizManager.ScoreNum == 0) //You reached the static ScoreNum value in the QuizManager script.
        ...

Method 2: (using Unity Inspector)

It looks like the method 1 actually.
To reach QuizManager script from Results class a variable should be added. So we can assign it in Unity Inspector.

public class Results : MonoBehaviour
    ...
    // public float ScoreNum; //Delete this
    public QuizManager quizManager; //Drag the QuizManager script here in the Inspector
    ...
    public IEnumerator Stars()
        ...
        if (quizManager.ScoreNum == 0) //Now you reached the ScoreNum value in the QuizManager script.
        ...

I suggest learning Singleton pattern for this kind of operations. Managers are usually unique in the game, and they are hold as DontDestroyOnLoad in Unity, and they are Singleton.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tar?k