Hi there! Bit confused by an error that seems like it shouldn’t be occurring in the first place; obviously I’m missing something. Briefly, I have a C# script on a game object in Unity with the following code:
using UnityEngine;
using System.Diagnostics;
using System.IO;
public class CallPython : MonoBehaviour {
// Use this for initialization
void Awake ()
{
Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "C:\\hi.py";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.Start();
FileInfo theSourceFile = new System.IO.FileInfo("C:\\hi_fy.txt");
StreamReader reader = theSourceFile.OpenText();
string txt;
while ( (txt = reader.ReadLine()) != null )
UnityEngine.Debug.Log("-->" + txt);
}
}
The script’s intended behavior is for it to:
A) wake up (so far so good!)
B) use the Process class to execute Python2.7, and use this to call an external Python file, hi.py (so far so good!)
C) read the contents of a file, hi_fy.txt, altered by the execution of hi.py (… not so good)
A and B work famously; if I comment out from “StreamReader…” on, then C# executes Python2.7 which executes hi.py and successfully alters the file hi_fy.txt. However, when I uncomment from StreamReader on, I get the error indicated in the title of this thread:
Type System.IO.FileInfo' does not contain a definition for OpenText’ and no extension method OpenText' of type System.IO.FileInfo’ could be found (are you missing a using directive or an assembly reference?)
Since the FileInfo type appears to contain exactly a definition for the method OpenText(), and this seems to be a very common way for Unity users to read data from files via C# scripts, I’m pretty confused!
What could I be doing that would cause C# to think that System.IO.FileInfo has no definition for OpenText?
No worries, Suddoha. I’d done exactly what you were saying in the deleted post a few iterations of the code ago anyway, haha.
Thanks for the reply, hpjohn!
Ah, yeah, I seem to have linked the wrong OpenText method above. I’ll fix that.
I guess I’m glad it runs for you, haha; thanks for letting me know that it does. This is the first bit of .Net I’ve tried calling without success; I guess I’ll try to reinstall things and go from there.
Tried reinstalling the .NET framework, no luck yet (identical error message). Maybe it’s some kind of issue with Unity knowing where to look for that particular library…
edit: Reading online, it sounds like maybe Unity/Mono ships with its own implementation of the .NET framework, so what I reinstalled was probably totally irrelevant to Unity? Going to try reinstalling Unity.
Final reply (unless someone has an actual solution!): not a clue what’s wrong with my install of C#'s I/O libraries still, so I switched the I/O code to a separate UnityScript with a slight delay built-in via coroutine:
import System.IO;
var fileName = "C:\\hi_fy.txt";
function Start () {
yield WaitForSeconds (0.1);
var sr = new StreamReader(fileName);
var fileContents = sr.ReadToEnd();
sr.Close();
var lines = fileContents.Split("\n"[0]);
for (line in lines) {
Debug.Log(line);
}
}
Would still love to know how to fix my actual problem, but this works fine for now.
The reason I ask about your build platform is that if your build settings are set to Windows Store App or Windows Phone, then you get to use the latest .NET from Microsoft instead.
The latest version of .NET has moved some namespaces, classes, and members around when compared to older .NET or Ximian Mono.
garth: Oh, I understand now - thanks. My build settings are currently set for either Webplayer or Windows Standalone (the bug shows regardless of which I’m using), although I’m not actually trying to build the game into an executable at any point.
Baste: I believe it’s a compile-time error, yes; it appears in the ‘Console’ window of Unity directly after I save my C# script file in Mono. Copy-pasting the code you provided (with the null FileInfo reference) results in the same error for me, for some reason.
to my code in an effort to suppress the Terminal window popping up when Python runs, but my Mono installation makes similar complaints about both WindowStyle and ProcessWindowStyle:
Assets/Scripts/CallPython.cs(18,68): error CS0234: The type or namespace name ProcessWindowStyle' does not exist in the namespace System.Diagnostics’. Are you missing an assembly reference?
Assets/Scripts/CallPython.cs(18,35): error CS1061: Type System.Diagnostics.ProcessStartInfo' does not contain a definition for WindowStyle’ and no extension method WindowStyle' of type System.Diagnostics.ProcessStartInfo’ could be found (are you missing a using directive or an assembly reference?)