Unity and “The associated script cannot be loaded” error

Sometimes, working with Unity can be a bit trying 😀

I was doing a fair amount of house-cleaning on a project where I was moving scripts around, changing file names etc. At some point, I apparently touched a file which was associated with game data.

Just to set the background, you can have objects in Unity which are sub-classes of ScriptableObject. These objects can be used as data containers which store data in your game — for instance, information about each level in your game — and can be loaded at runtime to change/configure aspects of your game.

This particular project had hundreds of these level data objects and I apparently moved the associated script file (the one which derived from ScriptableObject and indicated the values stored in the data file) in a way which made Unity disassociate the script file from the data files.

Hence the error.

I had not hit this particular error before, and on doing a bit of Googling, the only results I came up with for this particular error adviced either removing all your script files from your project and re-adding them, or removing all the .meta files from your project (or the scripts folder and sub-folders) and letting Unity re-create the .meta files.

In case you weren’t aware, Unity creates a file with a .meta extension for each file and folder in your project. These files — as the extensions indicates — contain metadata about each file and folder.

Unfortunately, the above solutions did not fix the issue for me 😢

So I tried taking another look at what was going on.

When I selected the data file in Unity, it indicated that it was missing the script file. So I thought, “Why not add the script file manually?” and so dragged the relevant script file on to the Unity inspector. This gave me access to the properties of the data file but now they were all empty!

I compared this file with the saved git commit from just before the change — free tip, always make a git commit, or backup your files in some way before you make changes like this 😃 — and realized that the data file had all its contents replaced with new information. But I also noticed that the new information included a file ID that pointed to the correct script file, something like this:m_Script: {fileID: 11500000, guid: 88afd11c802cdf6449faf33ed6e4e3bd, type: 3}

Also, I could open the data file in a text editor and see all this information for myself.

So I thought to open the .meta file for the script file and see what it said. The script .meta file had a guid for the file too — like this:fileFormatVersion: 2
guid: 94d46d6cca4b64568811321f29fcf3f2

It’s probably evident to you at this point that Unity assigns a guid to each file in the project to identify the files uniquely and that the data file has a pointer to the relevant script file and that’s how it identifies which script file the data file is associated with.

So, all I really had to do was replace the guid in the data file with the correct guid from the script’s current .meta file and all should be well, right?

It was at least worth trying.

I tested out my theory by closing the Unity project, changing the guid in one data file and then re-opening the project to see if I could see the old data. And I could! Joy! Joy!! Joy!!!

So then I did a full search and replace for the old guid in all the old data files and replaced them with the new guid and now, all my data files were usable again!

Hopefully, this helps somebody else who runs into similar issues 😃

Leave a Reply

Your email address will not be published. Required fields are marked *