ライフル銃の「残弾数」と「回復アイテム数」を画面に表示する




残弾数とアイテム数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
public static int shotBulletCount = 60;
public static int bulletItemCount = 0;
// ★追加
public Text bulletNum;
public Text bulletItemNum;
// ★追加
void Start(){
bulletNum.color = Color.white;
bulletNum.text = shotBulletCount + "/60";
bulletItemNum.text = "× " + bulletItemCount;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
if (shotBulletCount < 1) {
return;
}
shotBulletCount -= 1;
// ★追加
bulletNum.text = shotBulletCount + "/60";
bulletItemNum.text = "× " + bulletItemCount;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
public void AddBulletItem(int amount){
bulletItemCount += amount;
print ("ライフル弾の回復の権利" + bulletItemCount + "回");
// ★追加
bulletItemNum.text = "× " + bulletItemCount;
}
}



EscapeCombat(メモ)
他のコースを見る



残弾数とアイテム数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
public static int shotBulletCount = 60;
public static int bulletItemCount = 0;
// ★追加
public Text bulletNum;
public Text bulletItemNum;
// ★追加
void Start(){
bulletNum.color = Color.white;
bulletNum.text = shotBulletCount + "/60";
bulletItemNum.text = "× " + bulletItemCount;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
if (shotBulletCount < 1) {
return;
}
shotBulletCount -= 1;
// ★追加
bulletNum.text = shotBulletCount + "/60";
bulletItemNum.text = "× " + bulletItemCount;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
public void AddBulletItem(int amount){
bulletItemCount += amount;
print ("ライフル弾の回復の権利" + bulletItemCount + "回");
// ★追加
bulletItemNum.text = "× " + bulletItemCount;
}
}



ライフル銃の「残弾数」と「回復アイテム数」を画面に表示する