How do you destroy a GameObject during the build process?

I want to create multiple versions of gameobjects in my scene and depending on the build platform, different ones will be kept and the rest destroyed.

For example, I have 2 objects in the scene.

  1. Generic object set up for PC version
  2. Generic object set up for mobile version

Both would have a script attached with an enum to say which platform the object is intended for.

So far, I can destroy the unwanted gameobject on Awake() when the scene loads. But to really cut down the build size and performance I want to omit them entirely from the build.

When I make a build, I want Unity to destroy the objects that aren’t needed so they don’t even need to run their script on Awake().

I’ve seen this technique used in another project I was involved with, but I don’t have access to the code any more. How can I do it?

I would assume you’d make an editor script, and use something along the lines of this:

Then, i’d just destroy the asset you don’t want.