Search Unity

Invalid character in Path

Discussion in 'Scripting' started by Duncan, Jan 11, 2011.

  1. Duncan

    Duncan

    Joined:
    Jan 8, 2011
    Posts:
    30
    My program is getting an invalid character in path error. The path is:

    D:/Unity/Unity Projects/UnityTest/Assets/User Assets/General/objectList.txt

    The code reads
    var sr = new System.IO.StreamReader(path);

    I have used this code successfully so the problem is in the path string, I can't see any problem with it though. The path is

    var path = Application.dataPath + "/" + userAssetsFile + "/" + gamesList + "/" +objectListFile;

    There is also another problem that the string variables aren't updating when I change the string variables for example when I change objectlistFile to be blah.txt the path doesn't change, or it sometimes does but not always.

    What is going on?
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Try debug.logging out the path:

    var path = Application.dataPath + "/" + userAssetsFile + "/" + gamesList + "/" +objectListFile;
    Debug.Log("PATH: " + path);

    Beyond that, I would not be surprised if it does not support forward slashes like that. Try changing them all to backslashes.
     
  3. Duncan

    Duncan

    Joined:
    Jan 8, 2011
    Posts:
    30
    Thats what I already did, with your code i get

    PATH: D:/Unity/Unity Projects/UnityTest/Assets/User Assets/General/objectList.txt

    the other slashes is simply get
    PATH: D:/Unity/Unity Projects/UnityTest/Assets\User Assets\General\objectList.txt

    and still the same error
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Notice your other path still has forward facing slashes. Try this:

    var path = Application.dataPath + "/" + userAssetsFile + "/" + gamesList + "/" +objectListFile;
    path = path.Replace("/", "\\");
     
  5. Duncan

    Duncan

    Joined:
    Jan 8, 2011
    Posts:
    30
    PATH: D:\Unity\Unity Projects\UnityTest\Assets\User Assets\General
    \objectList.txt

    ArgumentException: path contains invalid characters

    Interestingly, var objectListFile = "Object List.txt"; and has done since before I posted this. It is not registering the changes and seems to have some stored copy of the variables in the object the script is attached to, and it is not refreshing when the script asset is changed.

    Also, if it helps with the diagnosis, when I paste the output into the forum post it puts a random endline in after the word general as above. This I assume is the invalid character but path = path.Replace("\n", ""); doesn't change that.
     
  6. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Try "\r". That's a good bet that that's the issue.
     
    gamer2300 likes this.
  7. Duncan

    Duncan

    Joined:
    Jan 8, 2011
    Posts:
    30
    Thanks a lot man, that solved it. About the object script not updating when the script changes, is there somewhere I should be reporting that as a bug? There is an easy workaround which is simply to delete the script off the object and then add it again, but I am fairly sure this is unintended behaviour.
     
  8. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Yeah, I'm not sure about that. I've never had that issue before. (maybe try giving Unity a few seconds after saving/closing the editor before building/playing the game?)
     
  9. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    31
    it's absurd i get this error andin my case if use for example path="data/nomefile.txt" or path="data\\nomefile.txt" inside the source it works (new streamreader(path)) without error, but if i use some simlar to path="data/"+varname+".txt" where varname="nomefile" it give this error. Only when i make path=path.Replace("\r","") it give right result without error.
    this is for me a BIG BUG that make me lost many time. it's not normal for me....
     
  10. HelixNGC7293

    HelixNGC7293

    Joined:
    Oct 26, 2016
    Posts:
    13
    Got the same issue. Solved by the same way. Thx!