how can i add a filter for names in my game,How could i add a filter for names?

i have a change name button in my game and if some ones name is a slur or something it shouldn’t be, how can i make it detect something if it has the variation of letters in the name. ex. if some ones name is N-word man i want it to filter the whole word because it detected the first 3 letters of the N-word

I have noticed this line of code in my travels:"Platform")

So if you play with it a bit, you can check for whatever a string StartsWith(). However thinking on this problem, it might not exactly StartWith, and might just contain. Then the other problem is that there could be 100 different versions, or versions of words that you might wish to limit in the future. So I would research into string readers, and also how to use a text file to write out all of the different variations you may come up with in the future. And have a set line of code that checks each of your lines within the text file, to check against what someone may have said in chat, or set as their name.

I personally use text files for things like names, as there can be many, and typing each into an Array/List is super time consuming. My text reader code looks something like this:

private TextAsset maleNames;

private string[] arrayMale;

void GetTextFiles()
        arrayMale = maleNames.text.Split(new string[] { ",", "

" }, StringSplitOptions.None);

And just simply place the text file from your assets into the appropriate slot within the inspector. And also take note, that any text files within your project assets can be modified within Visual Studio. My location was personally at the bottom, in another folder, beneath all my classes :slight_smile:

I normally don’t like to share complete solutions but here ya go, this is what i use to check names. It uses regular expressions to detect if a name is ok by a list of given forbidden words. While the solution posted by @wideeyenow_unity generally is a good idea, this solution here also allows you to easily handle situation where for example the “i” in the N-Word is exchanged by a “1”. (which in my opinion should also not be a valid name. It also checks if the user for example only entered white-spaces.

So here you go. As always with any code: Please don’t just blindly copy it, try to understand it!

using System.Text.RegularExpressions;

public class NameChecker
    private static readonly string[] forbiddenWords = 
           //put all forbidden words here like these example words:

    private static readonly string fillerCharacters = @"[\s\-_+*!]*";

    private static System.Collections.Generic.Dictionary<char, string[]> characterAliases = new System.Collections.Generic.Dictionary<char, string[]>()
        {'i', new string[] { "i","1" } },
        {'o', new string[] { "o","0" } },
        {'a', new string[] { "a","4" } },
        {'e', new string[] { "e","3" } },
        {'l', new string[] { "l","1" } },
        {'s', new string[] { "s","5" } },
        {'v', new string[] { "v","u" } },
        {'u', new string[] { "v","u" } },
        {'t', new string[] { "t","7" } },

    private const int minimumLength = 3;
    private const int maximumLength = 20;
    private static Regex emptyRegex = new Regex(@"\s*");

    public static bool checkIfNameOK(string name)
        if (string.IsNullOrEmpty(name))
            return false;
        name = name.ToLower().Trim();
        if (name.Length < minimumLength || name.Length > maximumLength)
            return false;
        if (emptyRegex.Matches(name).Count == 1)
            return false;
        foreach(string word in forbiddenWords)
            if (new Regex(buildRegex(word)).Matches(name).Count == 1)
                return false;
        return true;

    private static string buildRegex(string word)
        var newRegex = @".*";
        for(int i=0;i< word.Length;i++)
            if (characterAliases.ContainsKey(word*))*

newRegex += “[”;
foreach (string value in characterAliases[word*])*
newRegex += value;
newRegex += “]+”;
newRegex += word*;*
newRegex += fillerCharacters;
return newRegex;
Please note that this solution will also prohibit the user from using any characters which are contained in the variable fillerCharacters.
Let me know if you have any questions on this.