プレイヤーがウェイポイントに到達するたびに、ゲームで敵をスポーンさせようとしています。
現在、この機能が動作しています。プレイヤーが最初の道に到達すると、敵がスポーンします。画面上のすべての敵を倒した後、彼は先に進みます。ただし、2 番目のウェイ ポイントに到達すると、敵はスポーンしません。
現在、コリジョン クラスで次のコード行を呼び出しています。
Destroy(gameObject)
これは最初のウェイ ポイントでは機能しますが、コリジョン クラスがアタッチされているゲーム オブジェクトが破棄されたため、2 番目のウェイ ポイントでは何もスポーンしません。ただし、私の敵はすべてプレハブであり、破棄機能はプレハブのそのインスタンスのみを破棄すると考えていました。インスタンス化メソッドをいつ呼び出しても、そのインスタンスのみが破棄されます。
次の方法で敵をスポーンしています。
public GameObject SpawnEnemies()
{
Vector3 _position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
// instantiate particel system
Instantiate(_particle, _position, Quaternion.identity);
_clone = (GameObject)Instantiate(_enemy, _position, transform.rotation);
_ai = _clone.GetComponent<HV_BaseAI>();
_ai._waypoints = _wayPoints;
return _clone;
}
次に、衝突メソッドで次のコードを使用して、まだ生きている敵の数を調べています。
GameObject g, f; // enemies I want to spawn
g = GameObject.FindGameObjectWithTag("SectionController");
f = GameObject.FindGameObjectWithTag("SectionController");
HV_SectionController tempSectionControllerGroundEnemies, tempSectionControllerFlyingEnemies;
tempSectionControllerGroundEnemies = g.GetComponent<HV_SectionController>();
tempSectionControllerFlyingEnemies = f.GetComponent<HV_SectionController>();
tempSectionControllerGroundEnemies._numberOfGroundEnemies.Remove(gameObject); // remove enemies from list
tempSectionControllerFlyingEnemies._numberOfFlyingEnemies.Remove(gameObject);
//Destroy(gameObject);
_numberOfGroundEnemies = tempSectionControllerGroundEnemies._numberOfGroundEnemies.Count;
_numberOfFlyingEnemies = tempSectionControllerFlyingEnemies._numberOfFlyingEnemies.Count;
次に、次に進みたいときは、次のチェックを行います。
if (_groundEnemiesRemaining == 0)
{
MoveToNextSection();
_sectionStartTime = Time.time;
}
上記の行が現時点で 1 種類の敵のみをチェックしていることはわかっていますが、現時点で問題を抱えているのは地上の敵です。
最初のセクションからスポーンしている敵のプレハブを削除する方法を知っている人はいますか?
「GameObject」タイプのオブジェクトは破棄されましたが、まだアクセスしようとしています。