0

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;
    }
}
miha281
  • 31
  • 4
  • 4
    You really shouldn't be defining a separate set of classes for each quiz... You should be able to have a [game manager](https://stackoverflow.com/questions/13730112/unity-singleton-manager-classes) that has a list of `GameInstance`s like `List quizInstances;` , so you can do `quizInstances[i].Score`. So, you could reset the scores as necessary with `quizInstances[i].Score = 0;`. Each `Score` and `ScorePanel` object should have an int field `public int quizIndex;` that tells it what quiz it's in, so it can modify `GameManager.instance.quizInstances[quizIndex].Score` as needed. – Ruzihm Aug 27 '19 at 16:59
  • I add a new script @Ruzihm tell me if it is what u said , but I have an error – miha281 Aug 31 '19 at 12:34
  • I called it ResetButton instead Game Manager – miha281 Aug 31 '19 at 12:35

1 Answers1

0

After a completing a quiz(with 20) questions. Save and add the score in PlayerPref using PlayerPrefs.SetInt("totalScore",score); If you like to reset before completing just clear currentScore.Else if you completed the game ,then score=PlayerPrefs.GetInt("totalScore")+currentScore;.In next line again set the score using PlayerPrefs.SetInt("totalScore",score);

Udhaya
  • 335
  • 3
  • 12