複数ステージ⑧(ゲームデータをリセットする)



スコアデータをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score = 0;
private Text scoreLabel;
void Start () {
scoreLabel = this.gameObject.GetComponent<Text>();
scoreLabel.text = "Score " + score;
}
public void AddScore(int amount){
score += amount;
scoreLabel.text = "Score " + score;
}
// ★追加(スコアデータをリセットするメソッド)
public void ScoreReset(){
score = 0;
}
}

ゲームオーバーになったらデータをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour {
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP = 5;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public static int destroyCount = 0;
// ★追加
private ScoreManager scoreManager;
void Start(){
// ★追加(ScoreLabelオブジェクトに付いているScoreManagerスクリプトにアクセスする。)
scoreManager = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
UpdatePlayerIcons ();
playerHPSlider = GameObject.Find ("PlayerHPSlider").GetComponent<Slider> ();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("EnemyMissile")) {
playerHP -= 1;
playerHPSlider.value = playerHP;
Destroy (other.gameObject);
AudioSource.PlayClipAtPoint (damageSound, Camera.main.transform.position);
if (playerHP == 0) {
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate (effectPrefab, transform.position, Quaternion.identity) as GameObject;
Destroy (effect, 1.0f);
AudioSource.PlayClipAtPoint (destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★書き方変更&追加
if (destroyCount < 5) {
Invoke ("Retry", 1.0f);
} else {
destroyCount = 0; // ★追加(ゲームオーバーになったら残機数をリセットする。)
scoreManager.ScoreReset (); // ★追加(ゲームオーバーになったらスコアをリセットする。)
SceneManager.LoadScene("GameOver");
}
}
}
}
void UpdatePlayerIcons(){
for(int i = 0; i < playerIcons.Length; i++){
if (destroyCount <= i)
playerIcons [i].SetActive (true);
else
playerIcons [i].SetActive (false);
}
}
void Retry(){
this.gameObject.SetActive (true);
playerHP = 5;
playerHPSlider.value = playerHP;
}
public void AddHP(int amount){
playerHP += amount;
if (playerHP > 5)
playerHP = 5;
playerHPSlider.value = playerHP;
}
public void Player1Up(int amount){
destroyCount -= amount;
if (destroyCount < 0)
destroyCount = 0;
for(int i = 0; i < playerIcons.Length; i++){
if (destroyCount <= i)
playerIcons [i].SetActive (true);
else
playerIcons [i].SetActive (false);
}
}
}

Danmaku Ⅱ(基礎2/全22回)
他のコースを見る


スコアデータをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score = 0;
private Text scoreLabel;
void Start () {
scoreLabel = this.gameObject.GetComponent<Text>();
scoreLabel.text = "Score " + score;
}
public void AddScore(int amount){
score += amount;
scoreLabel.text = "Score " + score;
}
// ★追加(スコアデータをリセットするメソッド)
public void ScoreReset(){
score = 0;
}
}

ゲームオーバーになったらデータをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour {
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP = 5;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public static int destroyCount = 0;
// ★追加
private ScoreManager scoreManager;
void Start(){
// ★追加(ScoreLabelオブジェクトに付いているScoreManagerスクリプトにアクセスする。)
scoreManager = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
UpdatePlayerIcons ();
playerHPSlider = GameObject.Find ("PlayerHPSlider").GetComponent<Slider> ();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("EnemyMissile")) {
playerHP -= 1;
playerHPSlider.value = playerHP;
Destroy (other.gameObject);
AudioSource.PlayClipAtPoint (damageSound, Camera.main.transform.position);
if (playerHP == 0) {
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate (effectPrefab, transform.position, Quaternion.identity) as GameObject;
Destroy (effect, 1.0f);
AudioSource.PlayClipAtPoint (destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★書き方変更&追加
if (destroyCount < 5) {
Invoke ("Retry", 1.0f);
} else {
destroyCount = 0; // ★追加(ゲームオーバーになったら残機数をリセットする。)
scoreManager.ScoreReset (); // ★追加(ゲームオーバーになったらスコアをリセットする。)
SceneManager.LoadScene("GameOver");
}
}
}
}
void UpdatePlayerIcons(){
for(int i = 0; i < playerIcons.Length; i++){
if (destroyCount <= i)
playerIcons [i].SetActive (true);
else
playerIcons [i].SetActive (false);
}
}
void Retry(){
this.gameObject.SetActive (true);
playerHP = 5;
playerHPSlider.value = playerHP;
}
public void AddHP(int amount){
playerHP += amount;
if (playerHP > 5)
playerHP = 5;
playerHPSlider.value = playerHP;
}
public void Player1Up(int amount){
destroyCount -= amount;
if (destroyCount < 0)
destroyCount = 0;
for(int i = 0; i < playerIcons.Length; i++){
if (destroyCount <= i)
playerIcons [i].SetActive (true);
else
playerIcons [i].SetActive (false);
}
}
}

複数ステージ⑧(ゲームデータをリセットする)