Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
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.

05 Fév 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

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam