Tokyo, Japan |


How to implement a banned words filter in Unity

If you use a registration form in your Unity app, it’s a good idea to implement a profanity filter that prevents new users from signing up with usernames using prohibited words.

An easy way to do this is with a plain text file and some simple coding in C# using a List. This method will work with non-English characters as well, so you could use it to filter out words in German, French, Japanese, Chinese and other languages. It’s also cross-platform and works on desktops and mobile devices.

In your text file, put each word in its own line, like this:

…and so on.

It doesn’t matter what you name the file. I’ll call it BadWords.txt. Save this file anywhere under Assets folder in your Unity project.

Now, the coding part. The code will need access to other namespaces, so add them at the top of your script:

using System.Collections.Generic;
using System.Linq;

In your class, define a List and a public TextAsset variable:

List<string> badWords;
[SerializeField] TextAsset badWordsFile;

In one of your functions (you could put this in Awake), read all the words and insert them into the List:

badWords = new List<string>();
string[] lines = badWordsFile.text.Split('\n');
foreach (string line in lines)

For reading a text file line by line, people tend to use StreamReader or .Net classes but this is a much simpler way and it’s platform-independent.

Now in the Project window find BadWords.txt text file you created above and drag and drop it into the badWordsFile variable in the Inspector window.

Lastly, in your block of code where you validate user’s input, you can add word checking like so:

bool isBadWord = badWords.Any(userName.ToLower().Contains);
	// The name contains a bad word.
	// Show error, etc.
	// The name is OK.
	// Show confirmation, etc.

The above block will verify if the input (in this example “userName”) contains any of the prohibited words from the List, regardless of their position in the input. It also temporarily changes the input to lowercase to allow for case-insensitive search (for this to work, you must have all your words in the list written in lowercase). If you don’t need case-insensitivity, remove the ToLower() part.

Note: All code in this article is written in C#.


  • Ryan

    hey i was just wondering the code works fine while having a few lines of text but not working with a large list of words how can i fix this….ive tried finding a solution online….but havnt found anything

    • Nabil

      Hi, is showing some errors, can u help me?