I have two scripts. First script which is "Score" save highest score and tracking recent score. Second script is responsible for deactivating the panel, when highest score or recent score reaches value of 50. But I can't really find any good solution for this, so I hope somebody can help me. I tried many options such as PlayerPrefs.HasKey, PlayerPrefs.GetInt, but still can't get it.
I'll write my question here one more time, if it wasn't precise enough
How can I change my “PanelDeactivator” script, so I can deactivate panel in another script which is "PanelDeactivator" using highest score number? For example, when the highest score becomes 50, I want to deactivate panel. I can't find one code solutions for it.
Thank you in advance for your support!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _scoreText;
[SerializeField] private TextMeshProUGUI _highestScore;
public int _scoreNumber;
// Start is called before the first frame update
void Start()
{
_scoreNumber = 0;
_scoreText.text = "Score: " _scoreNumber;
_highestScore.text = PlayerPrefs.GetInt("Highest Score: ", 0).ToString();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("ScoreLine"))
{
Destroy(collision);
_scoreNumber = 1;
_scoreText.text = "Score: " _scoreNumber;
if(_scoreNumber > PlayerPrefs.GetInt("Highest Score: ", 0))
{
PlayerPrefs.SetInt("Highest Score: ", _scoreNumber);
_highestScore.text = _scoreNumber.ToString();
}
}
}
public void ResetHighScore()
{
PlayerPrefs.DeleteKey("Highest Score: ");
_highestScore.text = "0";
}
}
Second script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PanelDeactivator : MonoBehaviour
{
[SerializeField] private GameObject _firePanel;
[SerializeField] private GameObject _snowPanel;
[SerializeField] private GameObject _transparentPanel;
private Score _scoreScript;
[SerializeField] private int _scoreNumberScript;
void Start ()
{
_scoreScript = GetComponent<Score>();
}
public void OpenPanel1()
{
if(PlayerPrefs.HasKey("Highest Score: "))
{
_firePanel.SetActive(false);
}
}
}
CodePudding user response:
@blanshar you can save the high score in file using Json Serialization/Deserialization, along with custom bool to your file making it false, if the high-score is reached make the bool true. Now you can use this bool activate/deactivate the panel.
CodePudding user response:
If you write only the provided code, you can solve it with the code below.
However, I want you to know that this kind of code is not good.
Score.cs
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("ScoreLine"))
{
Destroy(collision);
_scoreNumber = 1;
_scoreText.text = "Score: " _scoreNumber;
if(_scoreNumber > PlayerPrefs.GetInt("Highest Score: ", 0))
{
PlayerPrefs.SetInt("Highest Score: ", _scoreNumber);
_highestScore.text = _scoreNumber.ToString();
if (PlayerPrefs.GetInt("Highest Score: ", 0) >= 50)
{
PanelDeactivator.Instance.DeactivatePanel();
}
}
}
}
PanelDeactivator.cs
public class PanelDeactivator : MonoBehaviour
{
public static PanelDeactivator Instance { get; private set; }
[SerializeField] private GameObject _firePanel;
[SerializeField] private GameObject _snowPanel;
[SerializeField] private GameObject _transparentPanel;
private Score _scoreScript;
[SerializeField] private int _scoreNumberScript;
void Start ()
{
_scoreScript = GetComponent<Score>();
}
public void DeactivatePanel()
{
_firePanel.SetActive(false);
_snowPanel.SetActive(false);
_transparentPanel.SetActive(false);
}
}
If there is an opportunity, I think it would be good to study JSON Serialization/Deserialization.
Hope your problem is solved :)