using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ResetButton : MonoBehaviour
{
public static ResetButton Instance;
public bool isGameOver = false;
public Score score;
public Text scoreText;
public Button yourButton;
internal static object instance;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else if (Instance != this)
{
Destroy(gameObject);
}
}
// Start is called before the first frame update
void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick()
{
ScorePanelUpdater.Score = 0;
}
// Update is called once per frame
void Update()
{
if (isGameOver && Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
I make a quiz game and I want my score to reset when you touch the button reset score. The quiz have different score scripts for 20 questions ( after you finish the first 20 questions , you start another quiz with different score for the next 20 questions ) I want a code that works with a specific Score script ( not to reset all scores from the game in the same time)
ScorePanel16:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ScorePanel16 : MonoBehaviour
{
// better if you can already reference this via Inspector
[SerializeField] Text text;
void Awake()
{
if (!text) text = GetComponent<Text>();
// it's always save to remove listeners before adding them
// makes sure it is added only once
GameInstance16.OnScoreChanged -= OnScoreChanged;
GameInstance16.OnScoreChanged += OnScoreChanged;
// invoke once now with current value
OnScoreChanged(GameInstance16.Score);
}
private void OnDestroy()
{
GameInstance16.OnScoreChanged -= OnScoreChanged;
}
private void OnScoreChanged(int newValue)
{
text.text = "Score: " + newValue;
}
}
GameInstance15:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class GameInstance15
{
// Start is called before the first frame update
private static int _score = 0;
// have an event to register listeners
public static event Action<int> OnScoreChanged;
// public property
public static int Score
{
get { return _score; }
set
{
_score = value;
// invoke an event to inform all registered listeners
OnScoreChanged?.Invoke(_score);
}
}
}
Score16:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score16 : MonoBehaviour
{
public Text scoreText;
public Button button;
// Use this for initialization
void Start()
{
DontDestroyOnLoad(gameObject);
if (!button) button = GetComponent<Button>();
button.onClick.AddListener(() => { GameInstance16.Score++; });
GameInstance16.OnScoreChanged -= OnScoreChanged;
GameInstance16.OnScoreChanged += OnScoreChanged;
// invoke once now with current value
Debug.LogFormat("Current Score: {0}", GameInstance16.Score);
OnScoreChanged(GameInstance16.Score);
}
private void OnDestroy()
{
GameInstance16.OnScoreChanged -= OnScoreChanged;
}
private void OnScoreChanged(int newValue)
{
scoreText.text = "Score: " + newValue;
}
// These two make not much sense .. since the value already was public
// you wouldn't need methods for this
// anyway now you could directly do it on GameInformation instead
public void AddScore(int s)
{
GameInstance16.Score += s;
}
public int GetScore()
{
return GameInstance16.Score;
}
}