Проблема с продолжением работы корутины.
Добавлено: 19 фев 2020, 19:31
Добрый день. Решил недавно сделать проект, не буду углубляться в детали, затрону лишь то, с чем проблема. Суть в том, что есть фигуры, которые движутся прямо (сделано через три спавнера). Четвертый спавнер раз в 30 секунд создает линию. Когда линия создается, спавн остальных фигур останавливается до того момента, пока линия не разрушится. Решил сделать через корутины. Когда линия разрушается, спавн фигур должен возобновиться. Но происходит это не совсем так, как я рассчитывал: из трех спавнеров начинает работать только один. Ниже код, где написан спавн и остановка спавна фигур.
Синтаксис:
Используется csharp
void Start()
{
manager = managerObj.GetComponent<gManager>();
coroutine = InstantiateObj(Random.Range(3f, 4f), manager.Line);
StartCoroutine(coroutine);
}
void Update()
{
if (manager.Line == true)
{
StopAllCoroutines();
coroutine = InstantiateObj(Random.Range(0f, 0f), true);
StartCoroutine(coroutine);
if (GameObject.FindGameObjectWithTag("Line") == null)
{
StopAllCoroutines();
manager.Line = false;
coroutine = InstantiateObj(Random.Range(3f, 4f), false);
StartCoroutine(coroutine);
}
}
}
private IEnumerator InstantiateObj(float _time, bool _line)
{
Debug.Log(_line);
while (_line == false)
{
Debug.Log(_time);
yield return new WaitForSeconds(_time);
tmp = Instantiate(objToSpawn[Random.Range(0, objToSpawn.Length)], transform.position, Quaternion.identity) as GameObject;
Destroy(tmp, 10f);
}
while (_line == true)
{
Debug.Log(_time);
yield return null;
}
}
}
{
manager = managerObj.GetComponent<gManager>();
coroutine = InstantiateObj(Random.Range(3f, 4f), manager.Line);
StartCoroutine(coroutine);
}
void Update()
{
if (manager.Line == true)
{
StopAllCoroutines();
coroutine = InstantiateObj(Random.Range(0f, 0f), true);
StartCoroutine(coroutine);
if (GameObject.FindGameObjectWithTag("Line") == null)
{
StopAllCoroutines();
manager.Line = false;
coroutine = InstantiateObj(Random.Range(3f, 4f), false);
StartCoroutine(coroutine);
}
}
}
private IEnumerator InstantiateObj(float _time, bool _line)
{
Debug.Log(_line);
while (_line == false)
{
Debug.Log(_time);
yield return new WaitForSeconds(_time);
tmp = Instantiate(objToSpawn[Random.Range(0, objToSpawn.Length)], transform.position, Quaternion.identity) as GameObject;
Destroy(tmp, 10f);
}
while (_line == true)
{
Debug.Log(_time);
yield return null;
}
}
}