Trying to figure out a public static for scoring

I am trying to make a 2d application that switches each scene with each correct button pressed and with each correct button pressed a point adds into the score text, and of course for each wrong one it takes a point away. I am trying to construct a public static through a GameControl script that assists my simple scoring script in carrying on over to each screen through the little exercise. Another thing is, is it uses a DontDestroyonLoad method and I of course cannot do that because my timer is tied down to a canvas. But, I am new and if anyone is knowledgeable in public statics of this nature, I would really appreciate the help.

Here is what I have tried on my scoring script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;

public class Test : MonoBehaviour
{
    public TMP_Text Score;
    public int scoreAmount = 0;
    public int CurrentScore { get; set; }
    public string scoreKey = "Score";
    private Scene activeScene;
    public string B0;
  
 
    // Start is called before the first frame update
    void Start()
  
  
    {
      
        scoreAmount = 0;
        Score = GetComponent<TMP_Text>();
     

     



    }




    void Update()

  
      
    {
      
        Score.text = scoreAmount.ToString();
    }

  
 

    public void AddScore()
    {
        scoreAmount += 1;
        Score.text = "" + scoreAmount;
        GameControl.control.score++;
        Debug.Log(GameControl.control.score);

    }

   public void SubtractScore()
    {
        scoreAmount -= 1;
    }
 
 
}

And then here is the GameControl script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameControl : MonoBehaviour
{
    public static GameControl control;

    public int score;

    private void Awake()
    {
        if (control = null)
        {
            control = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (control != this)
        {
            Destroy(gameObject);
        }
        {
          
        }
        {
          
        }
    }
}

You can see in the Public Void AddScore() part of the scoring script I am referencing to an integer that should have no clue about scoring in my opinion but part of this is from a tutorial that I found online. I think if the GameControl script was more fused with the Scoring script it may have more success. I think the logic is possibly on the right track, but any help is greatly appreciated.

I was able to do it very simply, I just had to translate it from another project into mine and I was surprised in how much I had gone through in how simple it was and it was just the areas where a player pref get and set had to be placed through a script.

void Start()
   
   
    {
       
        scoreAmount = 0;
        Score = GetComponent<TMP_Text>();
        scoreAmount = PlayerPrefs.GetInt("scoreKey");






    }




    void Update()

   
       
    {
       
        Score.text = scoreAmount.ToString();
        PlayerPrefs.SetInt("scoreKey", scoreAmount);
    }

You have a typo here:

in your if statement you assign null to control. Since control is of type “GameControl” and that’s a UnityEngine.Object derived type, the value null that comes out of that assignment directly translates to “false” in the if statement. So you will never enter the body of the if statement.

1 Like