As You know there is few ways to start a coroutine in unity:
-
Passing coroutine name:
StartCoroutine("Coroutine"); IEnumerator Coroutine() {}
-
Executing coroutine method:
StartCoroutine(Coroutine()); IEnumerator Coroutine() {}
Using the first one it is easy to make a mistake in coroutine name when writing string literal.
And if you would use the second one, you won't be able to stop the coroutine. Because you are creating an independent coroutine instance.
-
The first analyzer encourages you to start the coroutine using 'nameof' syntax:
StartCoroutine(nameof(Coroutine)); IEnumerator Coroutine() {}
So now you are able to call 'StopCoroutine' method with the expected result and avoid excessive mistakes.
Coroutines in Unity are often used in combinations with while(true)
block:
IEnumerator Coroutine()
{
while(true)
{
...
}
}
Although it is easy to forget to put yield return
statement inside while(true)
(that leads to Unity crash),
Visual Studio doesn't have any embedded analyzers that prevents you from such troubles. So the code above won't trigger any Visual Studio warnings or recommendations.
Code analyzers in this package would warn you about possible issues and suggest to put yield return null;
on the end of while
block:
IEnumerator Coroutine()
{
while(true)
{
...
yield return null;
}
}