1

I have been asked to maintain a Unity project that has been built and maintained by different developers. I have discovered source CSharp files that I am almost sure are not being used. How can I safely remove such unused files from the Unity Project? I fear that there may be usages somewhere in the UI design like . . .

enter image description here

. . . and I'll end up with broken references that are even harder to discover and fix.

EDIT: Using 'find references in scene', lists a whole bunch of references but I cannot find any link to the file in question. Here's an example . . .

enter image description here

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
remudada
  • 3,751
  • 2
  • 33
  • 60
  • Right click script in project window -> Find references in scene. The depicted reference doesn't matter for this, because that reference is to another object in the scene with an attached script (which **will** show up). – Draco18s no longer trusts SE Dec 18 '19 at 14:22
  • 2
    Does this answer your question? [Unity3D Editor: How can I find all usages of a given asset?](https://stackoverflow.com/questions/40172220/unity3d-editor-how-can-i-find-all-usages-of-a-given-asset) – Draco18s no longer trusts SE Dec 18 '19 at 14:51
  • 1
    @Draco18s imagine you have a prefab with a UnityEvent where you reference a ScriptableObject. None of the two is in any scene so `FindReferencesInScene` wouldn't cover it. Or well in general any script that sits on prefabs only. – derHugo Dec 18 '19 at 23:22
  • 1
    Maybe something like [this](https://coderwall.com/p/qwol5q/remove-unused-files-from-unity3d-project) helps you .. always make **Backups**! ;) – derHugo Dec 18 '19 at 23:25
  • @Draco18s I am not sure I follow what you mean. The class in question SnowFlakesStoreController.cs is not used in any other script and none of the results I get point to that class. In the screenshot above, the only script reference appears to be NavigationController and that has nothing to do with SnowFlakes. I am still now sure how FindRerencesInScene works with scripts? – remudada Dec 19 '19 at 06:46
  • @derHugo I think this is what I need. The FindReferenesInScene does not seem to work 100% for scripts, your plugin might help as long as I have Backups :-) – remudada Dec 19 '19 at 06:49
  • @remudada Uh? https://i.stack.imgur.com/KwihB.png One of those sure looks like a Flakes Controller (class name might not equal script file name, could be a subclass, unclear based on just the one screenshot). – Draco18s no longer trusts SE Dec 19 '19 at 15:07
  • @Draco18s, well bad code. The one I am searching for is SnowFlakesStoreController.cs, the one you pointed out is FlakesController.cs. Different classes with similar names, bad convention. – remudada Dec 19 '19 at 15:55

2 Answers2

5

You can right click a script in your project window > Find references in scene.

This will filter your Hierarchy to just GameObjects that have a reference to that script (Either directly attached as component, or through onClicks etc.)

Alternatively you can apply this filter yourself in the search of the Hierarchy like using: ref:PathFromAssets/ScriptName.cs. example: ref:Scripts/Player/PlayerMovement.cs.cs

[Find References in scene[2] Found references Button1 and Button2 have Test.cs referenced in the OnClick.

Remy
  • 4,843
  • 5
  • 30
  • 60
  • thanks for the answer, But I think there is something wrong with me. Updating my question – remudada Dec 18 '19 at 19:00
  • 1
    Yeah the results of `FindReferencesInScene` also often is a mystery to me .. I somehow assumed it only works well for actual assets like ScriptableObjects, Prefabs, Images etc .. sometimes it even lists results in the scene for scripts I just created and never referenced anywhere so far :'D – derHugo Dec 18 '19 at 23:17
  • 1
    Also imagine you have a prefab which refers to a ScriptableObject in an `onClick` event. Then the script would be actually used but none of the two are in any scene so `FindReferencesInScene` would not catch it... – derHugo Dec 18 '19 at 23:19
1

With the "Find references in scene" method (accepted answer) You will only get what it's called or beign instanciated by the scene elements only. Which means that every scripts that are called or invoqued by another script from/by your unity project and parameters will not appears.

So the best way is to trace it by putting a Debug.Log(); in the said script, within his awake method

private void Awake()
{
    Debug.Log("here");
}

Then you play your scene and look at the console. If the script is used ANYWHERE, you'll see something like this

enter image description here

Now you can click on it, and it will show the whole route beneath the console

enter image description here

In my case, I can tell it is instanciated within the XRBaseController.cs script. And I even can double click on it to open it and see what it's like

Richard
  • 994
  • 7
  • 26