- Home /
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?
If there are objects in your scene that should only be present for certain builds, you could have a script that removes certain objects based on the target platform.
https://docs.unity3d.com/$$anonymous$$anual/PlatformDependentCompilation.html
Answer by Cynikal · Apr 05, 2018 at 07:28 PM
I would assume you'd make an editor script, and use something along the lines of this:
https://docs.unity3d.com/ScriptReference/Build.IPreprocessBuild.OnPreprocessBuild.html
Then, i'd just destroy the asset you don't want.
Thanks! It looks like that's what I want, although it wasn't introduced until Unity 5.6 and I'm running 5.4.
I intend to upgrade the project soon (I'm reviving an old project you see). Until then, do you know of any other ways? The other project I mentioned was definitely not running on 5.6 or later.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Error with instantiate after build 1 Answer
Can't Build PC Standalone due to multiple namespace script errors but playable in editor 1 Answer
How to make object visible without instantiating it? 2 Answers
check If Building will collide with something before placed? 1 Answer