Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AutoFire-II · Jun 01, 2018 at 01:38 AM · editoreditor-scriptingfiledeletereload

"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.)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bennett_apps · Jun 01, 2018 at 02:46 AM 0
Share

When the error pops up, does your project and code still work? Or is it a show-stopping bug?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EditorGUI add SortingLayer-like list to custom Editor 1 Answer

Unity Editor script in DLL 1 Answer

Event for loading component in the editor 2 Answers

Get the highlighted variable in the inspector 0 Answers

Perform action before script compilation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges