UEA0002: DoNotUseStringMethods

October 2, 2019 ยท View on GitHub

PropertyValue
IdUEA0002
CategoryGC
SeverityInfo

Example

Code with Diagnostic

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        // UEA0002: Using string methods can lead to code that is hard to maintain
        gameObject.SendMessage("ApplyDamage", 5.0);
    }
}

public class Example2 : MonoBehaviour
{
    public void ApplyDamage(float damage)
    {
        print(damage);
    }
}

Code with Fix

Use of SendMessage, SendMessageUpwards, BroadcastMessage, Invoke or InvokeRepeating leads to code that is hard to maintain. Consider using UnityEvent, C# event, delegates or a direct method call.

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        var obj = GetComponent<Example2>();
        obj.ApplyDamage(5.0);
    }
}

public class Example2 : MonoBehaviour
{
    public void ApplyDamage(float damage)
    {
        print(damage);
    }
}