[Unity] Basic usage of coroutines (Collections) (for myself)

2 minute read

Introduction

Here, the technical part is written so that beginners can understand it somehow.

What is a coroutine?

What is a coroutine?
I explain here so that those who say that can understand the meaning of the word somehow.

[Unity] Coroutine

The main points are these three points.

①IEnumerator 
②yield
③StartCoroutine 

First look at the script below.
From now on, I will explain in order based on this script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tesycoltin : MonoBehaviour
{
    void Start()
    {
        StartCoroutine("Coroutine1");
    }

    IEnumerator Coroutine1()
    {
        Debug.Log("start");
        yield return new WaitForSeconds(3);
        Debug.Log("3 seconds have passed");
        yield return new WaitForSeconds(5);
        Debug.Log("5 seconds have passed");
    }

    void Update()
    {
        
    }
}

By the way, the execution result of this script was displayed as “Start” at the moment when it was executed on the console, “3 seconds passed” after 3 seconds, and “5 seconds passed” after 5 seconds.

“shot” 2020-09-02 17.23.37.png

①IEnumerator

IEnumerator "function name"()
    {

"Details of processing"

    }

First, create an IEnumerator type function.
After writing “IEnumerator”, write a function with your own name.
In the script I prepared, it is called “Coroutine 1”.
Then, write the contents of the coroutine processing in the function.

②yield

After creating the function in ①, the next step is to write the contents of the processing in it.
As a way of writing, add “yield” at the beginning and then write what kind of processing to do.

The script I prepared is

        Debug.Log("start");
        yield return new WaitForSeconds(3);
        Debug.Log("3 seconds have passed");
        yield return new WaitForSeconds(5);
        Debug.Log("5 seconds have passed");

It will be executed in order from the top, so if you explain in words
First, “Start” is displayed on the console.
next
「yield return new WaitForSeconds(3);」
This means waiting for 3 seconds and reuniting, so literally after 3 seconds, the next “3 seconds have passed” will be displayed on the console.
The one below also shows “5 seconds have passed” on the console after 5 seconds.

I want to be careful here
「yield return new WaitForSeconds(3);」
After the
「yield return new WaitForSeconds(5);」
Is written,
It does not mean that you wait 3 seconds and then restart, but then wait 5 seconds again, 3 seconds after the first execution, and 5 seconds after the first execution.

And other

・ Wait for 1 frame “yield return null;”
-“Yield break;” to stop / destroy coroutines

There are some, but I don’t write it in depth because I hope you can feel how to use it here.

③StartCoroutine

Finally, if you put “StartCoroutine” in the Start method and the name of the function you created earlier in parentheses like this, it will be executed safely.

    void Start()
    {
        StartCoroutine("Function name");
    }

the end.