Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3215. Unity Engine. Passing a method n…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V3215. Unity Engine. Passing a method name as a string literal into the 'StartCoroutine' is unreliable.

Feb 05 2025

The analyzer has detected that a string literal is passed as an argument to the Invoke, InvokeRepeating, CancelInvoke, StartCoroutine, or StopCoroutine methods of the UnityEngine.MonoBehaviour class, which is unreliable. If the passed string literal no longer corresponds to the method for some reason—in particular, if the method changes its name or becomes unavailable—the call will be ignored without any notification.

The example:

public class CoroutineAndInvoke : MonoBehaviour
{
  void Start()
  {
    Invoke("CreateEnemyMethod", 1f);
    StartCoroutine("DangerCheckCoroutine");
  }

  private void CreateEnemyMethod()
  {
    ....
  }

  private IEnumerator DangerCheckCoroutine()
  {
    ....
  }
}

In this case, the Invoke and StartCoroutine methods are called with string literals that correspond to the existing methods, CreateEnemyMethod and DangerCheckCoroutine. The script works correctly, but reliability is broken. As the result, the analyzer issues a level 3 warning.

To maintain reliability when calling these methods, use nameof to retrieve the method name. If you use StartCoroutine, call the coroutine directly.

The example with the maintained reliability:

public class CoroutineAndInvoke : MonoBehaviour
{
  void Start()
  {
    Invoke(nameof(CreateEnemyMethod), 1f);
    StartCoroutine(DangerCheckCoroutine());
  }
  ....
}

In this case, passing a non-existent method as an argument is prevented. Another advantage of using nameof and direct coroutine calls is the ability to rename methods during refactoring. The changes automatically update the arguments of the Invoke and StartCoroutine methods.

Here is an example where string literals do not correspond to the methods:

public class CoroutineAndInvoke : MonoBehaviour
{
  void Start()
  {
    Invoke("CreateEnemy", 1f);
    StartCoroutine("DangerCheck");
  }

  private void CreateEnemyMethod()
  {
    ....
  }

  private IEnumerator DangerCheckCoroutine()
  {
    ....
  }
}

In this case, the Invoke and StartCoroutine methods are ignored because no methods corresponding to the passed string literals are detected. The analyzer issues a level 1 warning. Such an error can occur, for example, due to a typo when writing the method name in the argument or when renaming the method.

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam