敵の作成④(敵のHPを画面に表示する)















敵のHPを画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★(追加)
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
// ★(追加)
private Slider slider;
// ★(追加)
private void Start()
{
// (ポイント)GameObject.Find ("○○")の使い方を覚えよう。名前でオブジェクトを指定できる。
slider = GameObject.Find("EnemyHPSlider").GetComponent<Slider>();
// スライダーの最大値の設定
slider.maxValue = enemyHP;
// スライダーの現在値の設定
slider.value = enemyHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Missile"))
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
enemyHP -= 1;
// ★(追加)この一行を追加しないとスライダーバーの目盛りが変化しません!
slider.value = enemyHP;
Destroy(other.gameObject);
if (enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(destroySound, transform.position);
}
}
}
}

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














敵のHPを画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★(追加)
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
// ★(追加)
private Slider slider;
// ★(追加)
private void Start()
{
// (ポイント)GameObject.Find ("○○")の使い方を覚えよう。名前でオブジェクトを指定できる。
slider = GameObject.Find("EnemyHPSlider").GetComponent<Slider>();
// スライダーの最大値の設定
slider.maxValue = enemyHP;
// スライダーの現在値の設定
slider.value = enemyHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Missile"))
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
enemyHP -= 1;
// ★(追加)この一行を追加しないとスライダーバーの目盛りが変化しません!
slider.value = enemyHP;
Destroy(other.gameObject);
if (enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(destroySound, transform.position);
}
}
}
}

敵の作成④(敵のHPを画面に表示する)