Strings and comparisons

Working with strings in Unity is common. Game objects have names and tags, animations have parameters, and games feature many other string properties, including names, localization data, character dialog, and more. Consequently, we often need to compare two strings, checking to see whether two words match--such as searching for objects by name, or checking words typed by a player in a dictionary. There are many ways to compare strings in code, but these vary in performance and speed, and the fastest method has not always remained constant across versions. As of Unity 5.5, the optimal method for comparing two strings for equality is as follows:

StringOne.Equals(StringTwo, StringComparison.Ordinal);

The most important ...

Get Mastering Unity 2017 Game Development with C# - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.