(方向性)
- Tagを使って、目的のオブジェクトを特定する。
- 「配列」と「foreach」の組み合わせで処理を行う。
(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HumanHP : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
public int Hp;
// ★追加
private GameObject[] targets;
public GameObject effectPrefab2;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Toge"))
{
Destroy(collision.gameObject);
GameObject effect = Instantiate(effectPrefab, collision.transform.transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Hp -= 1;
if (Hp == 0)
{
// ★追加
targets = GameObject.FindGameObjectsWithTag("Hana");
foreach(GameObject i in targets)
{
Destroy(i);
GameObject effect2 = Instantiate(effectPrefab2, i.transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
}
Destroy(transform.root.gameObject);
}
}
}
}