Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DirectoryNotFoundException with CSharpCodeProvider

Discussion in 'Scripting' started by Tiernan98, Aug 29, 2019.

  1. Tiernan98

    Tiernan98

    Joined:
    Jul 11, 2017
    Posts:
    42
    Hi,

    Basically I have Unity project that takes some C# code, and compiles it in memory and instantiates a class from this compiled assembly. This works completely fine on Windows and the code looks like this:
    Code (CSharp):
    1.        CSharpCodeProvider provider = new CSharpCodeProvider();
    2.  
    3.         CompilerParameters param = new CompilerParameters
    4.         {
    5.             GenerateExecutable = false,
    6.             GenerateInMemory = true
    7.         };
    8.  
    9.         // Means using statements will work
    10.         param.ReferencedAssemblies.Add(typeof(MonoBehaviour).Assembly.Location);
    11.         param.ReferencedAssemblies.Add(typeof(Experience).Assembly.Location);
    12.  
    13.        CompilerResults results = provider.CompileAssemblyFromSource(param, code);
    14.  
    15.         foreach (CompilerError e in results.Errors)
    16.         {
    17.             Debug.LogError(e.ErrorText);
    18.         }
    However, when this on Android I get the following error in the Android Logcat:
    Code (csharp):
    1. 2019/08/29 14:40:45.424 20040 20069 Error Unity DirectoryNotFoundException: Could not find a part of the path "/tmp/a6icqx2c.tmp".
    2. 2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00164] in <7d97106330684add86d080ecf65bfe69>:0
    3. 2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in <7d97106330684add86d080ecf65bfe69>:0
    4. 2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in <7d97106330684add86d080ecf65bfe69>:0
    5. 2019/08/29 14:40:45.424 20040 20069 Error Unity at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
    6. 2019/08/29 14:40:45.424 20040 20069 Error Unity at System.CodeDom.Compiler.TempFileCollection.EnsureTempNameCreated () [0x00076] in <0079a30f96a047348857e1cecc6c638a>:0
    7. 2019/08/29 14:40:45.424 20040 20069 Error Unity at System.Co
    Unfortunately the error is cut off, as you can see, but I think it seems obvious it is the CompileAssemblyFromSource(param, code) line.

    I have tried this: https://answers.unity.com/questions/364580/scripting-works-in-editor-try-it-but-not-build.html but it didn't make any difference.

    Has anyone any idea why this is happening?