*コルーチンの処理を「変数」に代入しておくことがポイント。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyGene2 : MonoBehaviour
{
public GameObject enemyPrefab;
public int geneAmount;
[Range(0, 5.00f)]
public float waitTime;
private ScoreManager scoreManager;
private int gene2Score;
// (ポイント)
// コルーチンを変数に代入する。
private IEnumerator ge;
void Start()
{
// *
ge = GeneEnemy();
StartCoroutine(ge);
scoreManager = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
void Update()
{
gene2Score = ScoreManager.score;
print(gene2Score);
// *
// もしもスコアが10になったらコルーチンを停止する。
if (gene2Score == 10)
{
StopCoroutine(ge);
}
}
IEnumerator GeneEnemy()
{
while (true)
{
for (int i = 0; i < geneAmount; i++)
{
GameObject enemy = Instantiate(enemyPrefab, transform.position, Quaternion.identity);
Destroy(enemy, 4.0f);
yield return new WaitForSeconds(waitTime);
}
yield return new WaitForSeconds(1.0f);
}
}
}