Prevent Xcode from removing an object as unused.

I’ve recently started getting an issue with xcode removing one of my “gameObject” while unloading unused assets to reduce memory usage.

The gameobject has a script on it which deals with my advertisement logic. The object is created only once on the startup and then it is used throughout the game by accessing its reference in other scripts. It also has “DontDestroyOnLoad” on it as well.
I’ve been using the same logic in a lot of games but recently it has started giving me an issue. Every time a new scene is loaded, Xcode unloads a bunch of unused assets and it removes this gameObject along with it. When i try to reference it I get “nullReference” exception.

I have placed logs to confirm that it is actually the xcode which is removing the object. I wanted to know if there is any way to stop Xcode from removing that gameObject.

I have also tried checking “Dead Code Stripping” to “no” in BuildingSettings tab of Xcode but it doesn’t help either as it doesn’t let me build without it.

Thank you for your help in advance.

Removal of game objects is done by the Unity engine, not XCode.

XCode stripping occurs at compile time and is nothing related to this problem.

Generally Unity will only unload a “Don’tDestroyOnLoad” object when you actually destroy it yourself with code.

First of all make sure the object is truly being created properly on the iPhone device. It may work fine in the editor and fine in Android, and yet fail in iPhone, but this will have to do with the advertising library you’re using, or something else, not with XCode.

1 Like

Thank you for the reply. I tried what i could see might be the issue but it didn’t work so instead I created new scripts and distributed them on different object as a workaround and now its working.

This may also help you.

Found a solution based from this thread:

You need to follow the "Simple checklist for making your distribution as small as possible" from this manual Unity - Manual: Optimizing the size of the built iOS Player

I hope this helps you.