シーン間でプレーヤーのHPを引き継ぐ方法
シーン間でHPデータを引き継ぐ方法
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TankHealth : MonoBehaviour {
// ★改良
// 「ゲーム開始時点のHP」を入れる箱
public float initialPlayerHP;
// ★改良(ポイント)
// 「現在のHP」を入れる箱
// 「static」を付けるとシーンが変わっても「箱の中の情報を保持」できる。
public static float currentPlayerHP = 30;
// ★この2つはコメントアウト
//public const int PlayerHP = 30;
//float HP = PlayerHP;
//public Text PlayerHPLabel;
public GameObject bombEffect1;
public GameObject bombEffect2;
private Slider slider;
void Start () {
//PlayerHPLabel.text = "HP: " + HP;
slider = GameObject.Find ("TankHPSlider").GetComponent<Slider> ();
// ★下記の2行を追加(ポイント)
// ゲーム開始時点は「currentPlayerHP」の情報を使う。
initialPlayerHP = currentPlayerHP;
slider.value = initialPlayerHP;
}
public void AddplayerHP(int amount){
// ★変更・・・「HP」を全て「currentPlayerHP」に変更する。
currentPlayerHP += amount;
if (currentPlayerHP >= 30)
currentPlayerHP = 30;
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
}
void OnTriggerEnter(Collider other){
if (other.CompareTag ("EnemeyShell")) {
currentPlayerHP = currentPlayerHP - 1;
Handheld.Vibrate ();
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
Destroy (other.gameObject);
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 3.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
//gameObject.SetActive (false);
Destroy (this.gameObject);
//Invoke ("SceneChange", 0.0f);
SceneManager.LoadScene ("GameOver");
}
} else if (other.CompareTag ("EnemeyShellBoss")) {
currentPlayerHP = currentPlayerHP - 1.5f;
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 1.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
// gameObject.SetActive (false);
Destroy (this.gameObject);
//Invoke ("SceneChange", 0.0f);
SceneManager.LoadScene ("GameOver");
}
} else if (other.CompareTag ("Mine")) {
currentPlayerHP = currentPlayerHP - 3f;
slider.value = currentPlayerHP;
} else if (other.CompareTag ("PlayerDestroy")) {
currentPlayerHP = currentPlayerHP - 3f;
slider.value = currentPlayerHP;
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 1.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
Destroy (this.gameObject);
SceneManager.LoadScene ("GameOver");
}
}
}
}
Unity Code Memo
他のコースを見るシーン間でHPデータを引き継ぐ方法
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TankHealth : MonoBehaviour {
// ★改良
// 「ゲーム開始時点のHP」を入れる箱
public float initialPlayerHP;
// ★改良(ポイント)
// 「現在のHP」を入れる箱
// 「static」を付けるとシーンが変わっても「箱の中の情報を保持」できる。
public static float currentPlayerHP = 30;
// ★この2つはコメントアウト
//public const int PlayerHP = 30;
//float HP = PlayerHP;
//public Text PlayerHPLabel;
public GameObject bombEffect1;
public GameObject bombEffect2;
private Slider slider;
void Start () {
//PlayerHPLabel.text = "HP: " + HP;
slider = GameObject.Find ("TankHPSlider").GetComponent<Slider> ();
// ★下記の2行を追加(ポイント)
// ゲーム開始時点は「currentPlayerHP」の情報を使う。
initialPlayerHP = currentPlayerHP;
slider.value = initialPlayerHP;
}
public void AddplayerHP(int amount){
// ★変更・・・「HP」を全て「currentPlayerHP」に変更する。
currentPlayerHP += amount;
if (currentPlayerHP >= 30)
currentPlayerHP = 30;
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
}
void OnTriggerEnter(Collider other){
if (other.CompareTag ("EnemeyShell")) {
currentPlayerHP = currentPlayerHP - 1;
Handheld.Vibrate ();
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
Destroy (other.gameObject);
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 3.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
//gameObject.SetActive (false);
Destroy (this.gameObject);
//Invoke ("SceneChange", 0.0f);
SceneManager.LoadScene ("GameOver");
}
} else if (other.CompareTag ("EnemeyShellBoss")) {
currentPlayerHP = currentPlayerHP - 1.5f;
//PlayerHPLabel.text = "HP: " + HP;
slider.value = currentPlayerHP;
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 1.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
// gameObject.SetActive (false);
Destroy (this.gameObject);
//Invoke ("SceneChange", 0.0f);
SceneManager.LoadScene ("GameOver");
}
} else if (other.CompareTag ("Mine")) {
currentPlayerHP = currentPlayerHP - 3f;
slider.value = currentPlayerHP;
} else if (other.CompareTag ("PlayerDestroy")) {
currentPlayerHP = currentPlayerHP - 3f;
slider.value = currentPlayerHP;
if (currentPlayerHP > 0) {
GameObject effect1 = Instantiate (bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy (effect1, 1.0f);
} else {
GameObject effect2 = Instantiate (bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy (effect2, 1.0f);
Destroy (other.gameObject);
Destroy (this.gameObject);
SceneManager.LoadScene ("GameOver");
}
}
}
}
シーン間でプレーヤーのHPを引き継ぐ方法