Script component broken after import.

I have multiple completed unity apps that I now need to combine into one. I exported them as a custom package, and then imported them into a new project. First two worked fine, but with the third, I’m seeing a number of gameobjects, not all, but many of my game object’s script components seems broken. The script is greyed out, I can’t use the enable button which is constantly shown as enabled, and I don’t see any of the variables you would normally see i the inspector. And, of course, it doesn’t run. I can double click on the script it shows. and it opens the script which looks perfect fine. If it remove the component and attempt to re-add the script, it says the class cannot be found. The script and class names match, and there are no compile errors. I can create a new script, copy the code from the existing one into this new one including the name, and I can put that one onto the gameobject, and it works fine, but when I leave the scene and come back, its back to broken.

Any suggestions?

NICE! I love Unity because you can do this stuff SO easily. I have done it MANY times with my own apps and with commercial apps as well.

I call it Operation Chimera whenever I combine apps.

Some steps to consider:

  1. use source control. Don’t even attempt it without source control.

  2. prepare each project FIRST… since you wrote both, they probably share some code, so isolate that code into separate folders within the game:

→ the game code
→ the shared code

  1. apply namespaces to the game portions of the code, and if you like, add a namespace to the shared code too. This is optional and depends on how divergent your shared code is.

  2. still before combining them, move each game into uniquely-named folders within itself (GameA and GameB) for instance.

4a. Harmonize shared stuff like tags, layers, sorting layers, physics settings, and any other engine-specific stuff you fiddled. By “harmonize” I mean “adjust both projects to separately use a superset of the shared features.”

  1. Create a new empty destination project, or pick one project as the final resting spot.

  2. Close Unity, then copy the ENTIRE contents of the Assets/ folder from both games into one project.

  3. Commit ALL of this to source control NOW!! If you don’t do this, you’re not being serious. It’s the only way to make forward progress when things get complicated.

  4. Now open Unity. Pay attention to the console, looking for collisions in GUIDs. If there are, write them down. You will potentially need to fix any prefabs that use these scripts.

After that it is just patching and fixing and fiddling, dupe classes, bugs etc.

Here is the skinny on how Unity connects stuff:

SCRIPTS OR ASSETS ARE MISSING OR BLANK

ALWAYS move all Unity assets by using the Unity Editor. Any other way risks disconnecting the GUID in the meta file from the original file, and this will result in all connections to that asset being broken.

Some info about Missing script warnings, broken prefabs, GUIDs, renaming GUIDs, etc:

https://discussions.unity.com/t/836322/2
https://discussions.unity.com/t/815173/7
https://discussions.unity.com/t/815173/9

EVERYTHING in Unity is connected to the above GUID, which is stored ONLY in the metafile, and hence why the metafiles ALWAYS MUST be source-controlled.

When Renaming: It is super-easy to inadvertently change the GUID by renaming outside of Unity. Don’t do that. Instead:

  • close Visual Studio (important!)
  • rename the file(s) in Unity
  • in Unity do Assets → Open C# Project to reopen Visual Studio
  • now rename the actual classes, and MAKE SURE THE FILE NAMES DO NOT CHANGE!

If you are NOT using source control while you do this, renaming files is an EXTREMELY dangerous process. Use source control at all times so that you can trivially revert if you miss a critical step and damage your project.

One more suggestion, assuming the new project uses a different Unity editor version than the older projects.

The project from which the content causes issues, make a copy of that. Then upgrade that project to the editor version you intend to import it into. Does everything still work? Fine, then extract it from this upgraded project and import it. This increases your chances of the exported assets still functioning in the new project.

This is all the more important the bigger the jump in versions. I wouldn’t even try to export a substantial amount of content from a Unity 5 project into Unity 6 (*) without first opening the old project with Unity 6, and fixing any issues.

Then the usual: delete the Library folder if the issue is inexplicable. But since there are no errors but the type cannot be found, if you are using Assembly Definitions you will either have to add dependencies (and possibly additional asmdefs) or remove all Asmdefs from the project so that all code goes in the same assembly. Another cause for this could be having code in a folder (or one of its parents) named “Editor” or “Plugins” - or any other kind of special folder name.

(*) Oh wow! :hushed: Putting it this way: That +1 version bump is actually spanning a decade!

2 Likes