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
-
Answer # 2
GManager
Remains in the singleton, so it will be carried over unless explicitly reset.
maybeRetryGame
The 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.
Related articles
- c # - how to transition from game scene to clear scene (2d)
- scene transition due to unity roulette results
- unity - pun2 scene transition timing shift
- about scene transition of unity, the scene does not transition normally
- unity - the game view after the scene transition is not displayed correctly
- unity how to load a scene when you win
- Unity C # package AssetBundle and scene detailed
- unity i want to refer to a variable set in another scene, but it doesn't work
- unity scene view does not show up in game view
- Unity implements scene switching progress bar display
- Unity UI or 3D scene to achieve the effect of following the mobile phone's gyroscope
- c # - transition to gameover scene
- Unity achieves karaoke lyrics transition effect
- unity - i want to put the character selected in the character selection scene into the game
- c # - [unity] how to judge scene source by tag or something
- unity - objects get stuck after a scene transition
- c # - want to run unity, idealens k4 sample scene
- unity - delay game over scene display
- c ++ - i don't know how to display timer in scene transition in opensiv3d
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
Added GManager.instance.StartGame () to the clear script before the title scene transition.
In GManager script
Reset is completed by adding.