- Home /
"Source file could not be found" error after editor script creates and deletes a cs file
I have an editor script that creates other scripts in the project. It needs to make a few scripts at a time, and throws an exception if it would end up overwriting anything. If it fails, I'm making it so that it will clean up any of the scripts that it made before it ran into the error.
.
My code works fairly well, but I'm running it a strange issue. If something goes wrong partway through the process, Unity says something like this:
error CS2001: Source file 'Assets/tmp/Editor/BoolRefEditor.cs' could not be found
.
I think that it's happening because the script listed in the error is getting created, found by Unity, and then deleted. Some time during the reload process, Unity must realize it's no longer there, and dumps the error into the Debug console.
.
I'm not sure how to fix this. I'm using StreamWriter to create the file, and then I'm loading the file using LoadAssetAtPath so that I may put a label on the file. Finally, in the case of an error, I use AssetDatabase.DeleteAsset to delete the files.
.
My first thought was to avoid using any of Unity's file management tools, so I tried removing the label-adding code and using File.Delete instead of AssetDatabase.DeleteAsset. However, the error message still showed up.
.
Because it's a fair amount of code, I've dumped the full file in hastebin. (Keep in mind that the first comment block is fairly old.)
.
But, in case that link comes down, here is the relevant code block:
try {
EditorApplication.LockReloadAssemblies();
foreach(TemplateInfo template in TemplateFileManager.Templates) {
string newScriptPath = CreateScriptFromTemplate(
readableName, typeName, referability,
template, targetPath, editorPath
);
if(!string.IsNullOrEmpty(newScriptPath)) {
newFilePaths.Add(newScriptPath);
}
} // End foreach(TemplateInfo template in Files.Templates)
} // End try
catch(System.Exception e) {
// TODO When this happens, Unity occasionally complains about
// a source file not being found. Somehow, it picks up the new
// scripts before they get deleted.
foreach(string path in newFilePaths) {
AssetDatabase.DeleteAsset(path);
/* This isn't any better
File.Delete(path);
*/
}
throw e;
}
finally {
EditorApplication.UnlockReloadAssemblies();
AssetDatabase.Refresh();
}
After creating the file using StreamWriter, CreateScriptFromTemplate has this code: // NOTE: I have tried commenting this out, but it hasn't helped
// To add labels, we need to load the object. To do that,
// it must be in the project view. Thus, we must reload.
AssetDatabase.Refresh();
UnityEngine.Object newFileObj =
AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(newFilePath);
AssetDatabase.SetLabels(newFileObj, new string[] { ScriptFileManager.CustomLabel } );
(Please forgive some of the unused stuff and overall silliness. I've recently finished with the bulk of this code and am cleaning it up.)
When the error pops up, does your project and code still work? Or is it a show-stopping bug?
Answer by AutoFire-II · Jun 01, 2018 at 05:52 PM
Thanks to Positive7 on the Unity Discord server for pointing me in the right direction on this one.
.
The problem comes from triggering a Refresh and then, while Unity loads, deleting them. I thought I had tested this before, but it turned out that Unity wasn't reloading scripts.
.
For everything that could get deleted later (within the same function call, anyway), it's best to avoid using AssetDatabase.Refresh. However, this also means that it's impossible to use any other AssetDatabase functions on new files, because they aren't in the project yet.
.
Fortunately, C# has plenty of alternatives. (See the File, Directory, and Path classes. If you're a few years in the future, look up more recent documentation.)
Answer by assouaalbert · Apr 19, 2019 at 01:52 AM
Greetings @AutoFire-II, I ma new here. I just started the Udacity course an I was instructed to download a file from the gitub rep. Which I did. And I encountered a similar problem when I tried playing a scene.
After reading this: "I think that it's happening because the script listed in the error is getting created, found by Unity, and then deleted. Some time during the reload process, Unity must realize it's no longer there, and dumps the error into the Debug console."
The main problem here is the anti virus or defender on your computer. I identifies some .cs files as treats and refuses to run them.
Deactivate the antivirus or defender on your computer and it will work. I am using avast and as soon as I noticed that just these files were not all found correct. That was the first thought on my mind. And it works now perfectly. Thank you.
Answer by marwi · Jul 09, 2020 at 11:27 AM
Just wait for the EditorApplication to finish update and compiling by checking EditorApplication.isUpdating
and EditorApplication.isCompiling
and deleting your scripts afterwards
Then you can use AssetDatabase.Refresh and all that like normal, just make sure to wait until the editor is not in the process of doing something script related.
Your answer
Follow this Question
Related Questions
Mark gameobject field as changed from prefab 2 Answers
Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer
Unity inspector scripting - scriptable objects - card game 0 Answers
How to properly handle Undo events in custom inspector? 0 Answers
How can i create a pup-up menu like the one for choose the shader on materials in unity 2019? 1 Answer