Illegal characters in path

I’m working on my first localization system for the game, but there is a problem I can’t resolve. I’m always getting an “illegal characters in path” exception. Here is my main localization class logic. I’ve commented out the error down the lines in the LoadLocalization method. That’s the only problem left with my system! I’m using simple .txt files sitting inside the StreamingAssets folder for the localization.

Please, help me understand what’s wrong!

Best regards, Vyacheslav.

using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class TextLocalizer : MonoBehaviour
{
private Dictionary<string, string> _localization;
private readonly string _missingTextString = “Localized text not found!”;

public static TextLocalizer Instance { get; private set; }

public bool IsReady { get; private set; }

private void Awake()
{
    IsReady = false;

    if (Instance == null) Instance = this;
    else if (Instance != this) Destroy(gameObject);

    DontDestroyOnLoad(gameObject);

    LoadLocalization("English.txt");

    IsReady = true;
}

public void LoadLocalization(string fileName)
{
    _localization = new Dictionary<string, string>();
    string filePath = Path.Combine(Application.streamingAssetsPath, fileName); // <= here's the error!

    if (File.Exists(filePath))
    {
        StreamReader reader = new StreamReader(filePath);
        string line;

        while (true)
        {
            line = reader.ReadLine();

            if (line == null) break;

            string[] words = line.Split('=');
            _localization.Add(words[0], words[1]);
        }

        reader.Close();
    }
}

public string GetLocalizedText(string key)
{
    string result = _missingTextString;
    if (_localization.TryGetValue(key, out string value)) result = value;
    return result;
}

}

Chances are that either fileName or Application.streamingAssetsPath contains a character that is not allowed in paths. Stick to the ordinary english alphabet and numbers to be sure. An easy way to check them is to Debug.Log the variables before you use them.

I don’t know what you put into fileName, but I think Application.streamingAssetsPath used to make use of Company Name or Product Name from the Unity’s player settings (Edit -> Project Settings -> Player Settings).

With a fresh mind I was able to address this apparent issue. As you said, I just Debug.Log(filePath) and find out that property Application.streamingAssetsPath returns a directory path string with symbols ‘\’ instead of ‘/’. So Instead of performing this “raw” call Application.streamingAssetsPath, I called this: Application.streamingAssetsPath.Replace(’/’, ‘\’). So the full and correct localization text file path looks like this:

string filePath = Path.Combine(Application.streamingAssetsPath.Replace(’/’, ‘\’), fileName).

File names are just language names with a .txt extension: English.txt, French.txt, Deutch.txt, etc.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.