Exclude scripts from build

I have some scripts that are for dev use only. They are attached to objects in a scene to do things with controls in the inspector.

I want to automatically exclude these scripts from a build.
It seems there are a few ways to do this, but none seem to be ideal.

  1. Place scripts in the editor folder.
    This doesn’t work because scripts in the editor folder can’t be attached to game objects.

  2. Use assembly definition to include only editor.
    Doesn’t work for the same reason as 1. Scripts can’t be attached to game objects.

  3. use #if ( UNITY_EDITOR )
    Works, but now the whole script is turned grey in VS and this doesn’t lend itself to editing very well.

Is there another way to do this? Alternately, is there a way to keep code readable using #if ( UNITY_EDITOR )?

1 Like

If you’re using the #if UNITY_EDITOR and #endif, the code between it will not be grey in VS if you’re in the editor.

I’m using both UnityEngine and UnityEditor.
I just tried it again, and now it’s not gray, but it was before. I dunno why.

Bump, Is there a way to exclude a script from build without having to use one of the 3 point above?

Something like exclude Unity Object from build at build time… or a special Tag?

I didn’t see your post last week… so I have created a duplicate :confused: It is scary my post was almost create the 25 may one year later!

There is something with the month of May and Excluding object from build… XD

People have been asking about this for decades. It’s probably not going to happen so plan accordingly. Here’s how:

Just use source control and delete the files you don’t want. You can instantly bring them back anytime you like.

Alternately, use source control and move the files to some other folder outside of the Unity Assets/ area.

Work WITH your tooling, not against it.

PROPERLY CONFIGURING AND USING ENTERPRISE SOURCE CONTROL

I’m sorry you’ve had this issue. Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

As far as configuring Unity to play nice with git, keep this in mind:

I usually make a separate repository for each game, but I have some repositories with a bunch of smaller test games.

Here is how I use git in one of my games, Jetpack Kurt:

Using fine-grained source control as you work to refine your engineering:

Share/Sharing source code between projects:

Setting up an appropriate .gitignore file for Unity3D:

Generally the ONLY folders you should ever source control are:

Assets/
ProjectSettings/
Packages/

NEVER source control Library/ or Temp/ or Logs/
NEVER source control anything from Visual Studio (.vs, .csproj, none of that noise)

Setting git up with Unity (includes above .gitignore concepts):

It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It’s simply ridiculous not to back up.

If you plan on joining the software industry, you will be required and expected to know how to use source control.

“Use source control or you will be really sad sooner or later.” - StarManta on the Unity3D forum boards

Shameless plug, there is an asset (by me) for that :wink:

It originated in this thread:
Excluding folders from build page-2

1 Like