Home>

I was able to move from the clear scene to the title scene,
If i play the game again from there
The remaining machines, scores, etc. are still inherited from the previous time and will not be initialized.
How will it be initialized?

Clear screen script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Clear: MonoBehaviour
{
    [Header ("Fade")] public FadeImage fade;
    [Header (SE that sounds when the button is pressed)] public AudioClip start SE;
    private bool firstPush = false;
    private bool goNextScene = false;
    public void PressStart ()
    {
        Debug.Log ("Press Start!");
        if (! firstPush)
        {
            GManager.instance.PlaySE (startSE);
            fade.StartFadeOut ();
            firstPush = true;
        }
    }
    private void Update ()
    {
        if (! goNextScene&&fade.IsFadeOutComplete ())
        {
            SceneManager.LoadScene ("TitleScene");
            goNextScene = true;
        }
    }
}

GManager script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GManager: MonoBehaviour
{
    public static GManager instance = null;
    [Header ("Score")] public int score;
    [Header ("Current Score")] public int stageNum;
    [Header ("current return position")] public int continueNum;
    [Header ("Current Remaining Machine")] public int heartNum;
    [Header ("default remaining machine")] public int defaultHeartNum;
    [HideInInspector] public bool isGameOver = false;
    private AudioSource audioSource = null;
    private void Awake ()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad (this.gameObject);
        }
        else else
        {
            Destroy (this.gameObject);
        }
    }
    private void Start ()
    {
        audioSource = GetComponent  ();
    }
    ///<summary>  /// Increase the remaining machine by one
    ///</summary>  public void AddHeartNum ()
    {
        if (heartNum<99){
            ++ heartNum;
        }
    }
    ///<summary>  /// Reduce the remaining machine by one
    ///</summary>  public void SubHeartNum ()
    {
        if (heartNum>0)
        {
            --heartNum;
        }
        else else
        {
            isGameOver = true;
        }
    }
    ///<summary>  /// Processing when starting from the beginning
    ///</summary>  public void RetryGame ()
    {
        isGameOver = false;
        heartNum = defaultHeartNum;
        score = 0;
        stageNum = 1;
        continueNum = 0;
    }
    ///<summary>  /// Sound SE
    ///</summary>  public void PlaySE (Audio Clip clip)
    {
        if (audioSource! = null)
        {
            audioSource.PlayOneShot (clip);
        }
        else else
        {
            Debug.Log ("Audio source not set");
        }
    }
}

Title scene script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Title: MonoBehaviour
{
    [Header ("Fade")] public FadeImage fade;
    [Header ("SE that sounds at the start of the game")] public AudioClip start SE;
    private bool firstPush = false;
    private bool goNextScene = false;

    public void PressStart ()
    {
        Debug.Log ("Press Start!");
        if (! firstPush)
        {
            GManager.instance.PlaySE (startSE);
            fade.StartFadeOut ();
            firstPush = true;
        }
    }
    private void Update ()
    {
        if (! goNextScene&&fade.IsFadeOutComplete ())
        {SceneManager.LoadScene ("stage1");
            goNextScene = true;
        }
    }
}


Score script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score: MonoBehaviour
{
    private Text scoreText = null;
    private int oldScore = 0;
    void Start ()
    {
        scoreText = GetComponent<Text>();
        if (GManager.instance! = null)
        {
            scoreText.text = "Score" + GManager.instance.score;
        }
        else else
        {
            Debug.Log ("I left the game manager behind!");
            Destroy (this);
        }
    }
    void Update ()
    {
        if (oldScore! = GManager.instance.score)
        {
            scoreText.text = "Score" + GManager.instance.score;
            oldScore = GManager.instance.score;
        }
    }
}

Script of the remaining machine

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Heart: MonoBehaviour
{
    private Text heartText = null;
    private int oldHeartNum = 0;
    void Start ()
    {
        heartText = GetComponent<Text>();
        if (GManager.instance! = null)
        {
            heartText.text = "×" + GManager.instance.heartNum;
        }
        else else
        {
            Debug.Log ("I left the game manager behind!");
            Destroy (this);
        }
    }
    void Update ()
    {
        if (oldHeartNum! = GManager.instance.heartNum)
        {
            heartText.text = "×" + GManager.instance.heartNum;
            oldHeartNum = GManager.instance.heartNum;
        }
    }
}
  • Answer # 1

    Added GManager.instance.StartGame () to the clear script before the title scene transition.
    In GManager script

    public void StartGame ()
        {
            isGameOver = false;
            heartNum = defaultHeartNum;
            score = 0;
            stageNum = 1;
            continueNum = 0;
        }


    Reset is completed by adding.

  • Answer # 2

    GManagerRemains in the singleton, so it will be carried over unless explicitly reset.
    maybeRetryGameThe data is reset when you use the process of, right? So I think it's better to add a process to reset variables that must be reset in the process after clearing.