1)TankのHP表示
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TankHealth : MonoBehaviour {
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
//public Text tankHPLabel;
// ★下記に修正
private GameObject tankHPLabel;
private Text thl;
void Start(){
//tankHPLabel.text = "" + tankHP;
// ★下記に修正
tankHPLabel = GameObject.Find("TankHPLabel");
thl = tankHPLabel.GetComponent<Text>();
thl.text = "" + tankHP;
}
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("EnemyShell")){
tankHP -= 1;
//tankHPLabel.text = "" + tankHP;
// ★下記に修正
thl.text = "" + tankHP;
if(tankHP > 0){
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity) as GameObject;
Destroy(effect1, 1.0f);
Destroy(other.gameObject);
} else {
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity) as GameObject;
Destroy(effect2, 1.0f);
Destroy(other.gameObject);
gameObject.SetActive(false);
//Destroy(gameObject,3.0f);
Invoke("SceneChange", 3.0f);
}
}
}
void SceneChange(){
SceneManager.LoadScene("Main");
//Application.LoadLevel("Main");
}
// ★★
public void AddHP(int amount){
// (復習)
tankHP += amount;
//tankHPLabel.text = "" + tankHP;
// ★下記に修正
thl.text = "" + tankHP;
}
}
2)Tankのショットカウント表示
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour {
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
//public Text shellLabel;
// ★下記に修正
private GameObject shellLabel;
private Text sl;
void Start(){
//shellLabel.text = "" + shotCount;
// ★下記に修正
shellLabel = GameObject.Find("ShellLabel");
sl = shellLabel.GetComponent<Text>();
sl.text = "" + shotCount;
}
void Update () {
if(Input.GetButtonDown("Fire1")){
//shotCount -= 1;
//AudioSource.PlayClipAtPoint(shotSound, transform.position);
if(shotCount < 1)
return;
shotCount -= 1;
//shellLabel.text = "" + shotCount;
// ★下記に修正
sl.text = "" + shotCount;
Shot();
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
public void Shot(){
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity) as GameObject;
Rigidbody shellRigidbody = shell.GetComponent<Rigidbody>();
shellRigidbody.AddForce(transform.forward * shotSpeed);
Destroy(shell, 2.0f);
}
// 砲弾数を増加させるメソッド
public void AddShell(int amount){
shotCount += amount;
//shellLabel.text = "" + shotCount;
// ★下記に修正
sl.text = "" + shotCount;
}
}