Answer the question
In order to leave comments, you need to log in
Unity(C#) - Why don't coroutines work?
I recently started learning about coroutines. I don't quite understand how they work. I was writing a boss script for a game, and using coroutines (for the first time). I needed that, when moving to the second stage, the boss moved according to the scheme with coroutines, but when moving to the second stage, he continued to move in the same way as in the first. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss2 : Monster
{
[SerializeField]
private float speed = 2.0F;
[SerializeField]
public float PosMax1;
[SerializeField]
public float PosMin1;
[SerializeField]
public float PosMax2;
[SerializeField]
public float PosMin2;
[SerializeField]
int HitPoints = 7;
[SerializeField]
int Stadium = 1;
Vector3 direction;
private SpriteRenderer sprite;
private Bullet bullet;
IEnumerator Waiting()
{
yield return new WaitForSeconds(5);
}
IEnumerator Start1()
{
yield return StartCoroutine(Waiting());
transform.position+=Vector3.left*10*Mathf.Sin(Time.deltaTime);
Debug.Log ("Complite");
}
IEnumerator Start2()
{
yield return StartCoroutine(Waiting());
if (transform.position.x >= PosMax2)
{
direction *= -1.0F;
}
else if (transform.position.x <= PosMin2)
{
direction = transform.right;
}
Debug.Log ("Complite");
}
IEnumerator Start3()
{
yield return StartCoroutine(Waiting());
transform.position+=Vector3.left*-10*Mathf.Sin(Time.deltaTime);
Debug.Log ("Complite");
}
IEnumerator Start4()
{
yield return StartCoroutine(Waiting());
if (transform.position.x >= PosMax1)
{
direction *= -1.0F;
}
else if (transform.position.x <= PosMin1)
{
direction = transform.right;
}
Debug.Log ("Complite");
}
void Start5()
{
if (transform.position.x >= PosMax1)
{
direction *= -1.0F;
}
else if (transform.position.x <= PosMin1)
{
direction = transform.right;
}
Debug.Log ("Complite");
}
protected override void Awake()
{
sprite = GetComponentInChildren<SpriteRenderer> ();
bullet = Resources.Load<Bullet>("Bullet");
}
protected override void Start()
{
direction = transform.right;
Stadium = 1;
}
void Update ()
{
Move ();
}
public void Damaged()
{
HitPoints--;
if (Stadium == 1 && HitPoints == 0)
{
Stadium = 2;
HitPoints = 10;
}
else if (Stadium == 2 && HitPoints == 0)
{
Destroy (gameObject);
}
}
protected virtual void OnTriggerEnter2D(Collider2D collider)
{
Bullet bullet = collider.GetComponent<Bullet> ();
if (bullet)
{
Damaged ();
}
Character character = collider.GetComponent<Character> ();
if (character)
{
character.ReceiveDamage ();
}
}
private void Move()
{
if (Stadium == 1)
{
if (transform.position.x >= PosMax1)
{
direction *= -1.0F;
}
else if (transform.position.x <= PosMin1)
{
//transform.position+=Vector3.up*10*Time.deltaTime;
direction = transform.right;
}
}
else if (Stadium == 2)
{
for (; ;)
{
Start5 ();
Start1 ();
Start2 ();
Start3 ();
Start4 ();
}
}
transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x > 0.0F;
}
}
Answer the question
In order to leave comments, you need to log in
Why did you put five starts into an endless loop? You will never get out of there.
Coroutines are launched using StartCoroutine, you use this method only to start Waiting, and you start starts just like regular methods, which is wrong. Better take an empty scene and experiment with coroutines there, connect with a debugger and see what happens. And wait, put them into each other until you figure it out.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question