親を名前で直接指定して破壊する方法
名前で直接特定して破壊する方法
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
private GameObject scoreManager;
public int scoreValue;
public GameObject bombEffect1;
public GameObject bombEffect2;
public int enemyHP;
private GameObject parent;
void Start(){
scoreManager = GameObject.Find("SM");
// ★(改良)「親」オブジェクを名前で直接指定する。
//parent = transform.root.gameObject;
parent = GameObject.Find("PursuitEnemyTypeC");
}
public GameObject[] ItemPrefab;
void OnTriggerEnter(Collider other){
if (other.CompareTag("Shell")){
enemyHP -= 1;
Destroy(other.gameObject);
if(enemyHP > 0){
GameObject effect1 = Instantiate(bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy(effect1, 7);
} else {
GameObject effect2 = Instantiate(bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy(effect2, 7);
Destroy(parent);
GameObject dropItem = ItemPrefab [Random.Range (0, ItemPrefab.Length)];
Vector3 pos = transform.position;
pos.y = 0.5f;
transform.position = pos;
Instantiate (dropItem, transform.position, Quaternion.identity);
ScoreManager sm = scoreManager.GetComponent<ScoreManager>();
sm.addScore (scoreValue);
}
}
}
}
Unity Code Memo
他のコースを見る名前で直接特定して破壊する方法
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
private GameObject scoreManager;
public int scoreValue;
public GameObject bombEffect1;
public GameObject bombEffect2;
public int enemyHP;
private GameObject parent;
void Start(){
scoreManager = GameObject.Find("SM");
// ★(改良)「親」オブジェクを名前で直接指定する。
//parent = transform.root.gameObject;
parent = GameObject.Find("PursuitEnemyTypeC");
}
public GameObject[] ItemPrefab;
void OnTriggerEnter(Collider other){
if (other.CompareTag("Shell")){
enemyHP -= 1;
Destroy(other.gameObject);
if(enemyHP > 0){
GameObject effect1 = Instantiate(bombEffect1, transform.position, Quaternion.identity) as GameObject;
Destroy(effect1, 7);
} else {
GameObject effect2 = Instantiate(bombEffect2, transform.position, Quaternion.identity) as GameObject;
Destroy(effect2, 7);
Destroy(parent);
GameObject dropItem = ItemPrefab [Random.Range (0, ItemPrefab.Length)];
Vector3 pos = transform.position;
pos.y = 0.5f;
transform.position = pos;
Instantiate (dropItem, transform.position, Quaternion.identity);
ScoreManager sm = scoreManager.GetComponent<ScoreManager>();
sm.addScore (scoreValue);
}
}
}
}
親を名前で直接指定して破壊する方法