Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Trying to figure out a public static for scoring

Discussion in 'Scripting' started by justinmed, Apr 1, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using TMPro;
    7. using Unity.VisualScripting;
    8. using UnityEngine.SceneManagement;
    9. using Random = UnityEngine.Random;
    10.  
    11. public class Test : MonoBehaviour
    12. {
    13.     public TMP_Text Score;
    14.     public int scoreAmount = 0;
    15.     public int CurrentScore { get; set; }
    16.     public string scoreKey = "Score";
    17.     private Scene activeScene;
    18.     public string B0;
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.  
    24.  
    25.     {
    26.      
    27.         scoreAmount = 0;
    28.         Score = GetComponent<TMP_Text>();
    29.      
    30.  
    31.      
    32.  
    33.  
    34.  
    35.     }
    36.  
    37.  
    38.  
    39.  
    40.     void Update()
    41.  
    42.  
    43.      
    44.     {
    45.      
    46.         Score.text = scoreAmount.ToString();
    47.     }
    48.  
    49.  
    50.  
    51.  
    52.     public void AddScore()
    53.     {
    54.         scoreAmount += 1;
    55.         Score.text = "" + scoreAmount;
    56.         GameControl.control.score++;
    57.         Debug.Log(GameControl.control.score);
    58.  
    59.     }
    60.  
    61.    public void SubtractScore()
    62.     {
    63.         scoreAmount -= 1;
    64.     }
    65.  
    66.  
    67. }
    68.  
    69.  

    And then here is the GameControl script:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameControl : MonoBehaviour
    7. {
    8.     public static GameControl control;
    9.  
    10.     public int score;
    11.  
    12.     private void Awake()
    13.     {
    14.         if (control = null)
    15.         {
    16.             control = this;
    17.             DontDestroyOnLoad(gameObject);
    18.         }
    19.         else if (control != this)
    20.         {
    21.             Destroy(gameObject);
    22.         }
    23.         {
    24.          
    25.         }
    26.         {
    27.          
    28.         }
    29.     }
    30. }
    31.  
    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.
     
  2. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    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.

    Code (CSharp):
    1. void Start()
    2.    
    3.    
    4.     {
    5.        
    6.         scoreAmount = 0;
    7.         Score = GetComponent<TMP_Text>();
    8.         scoreAmount = PlayerPrefs.GetInt("scoreKey");
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.     }
    16.  
    17.  
    18.  
    19.  
    20.     void Update()
    21.  
    22.    
    23.        
    24.     {
    25.        
    26.         Score.text = scoreAmount.ToString();
    27.         PlayerPrefs.SetInt("scoreKey", scoreAmount);
    28.     }
    29.  
    30.    
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    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.
     
    spiney199 likes this.