# Collisions on editor mode, or saving changes in play mode through script

I have a prefab that has a collider. This collider serves as an obstacle. The thing is that I want to remove this collider from all objects inside another collider defining the map limits.

This way, objects inside the game area will keep their collider, but those outside the play area get their collider removed, and serve just as decoration.

I can't use the OnTriggerEnter2D as physics are disabled in editor mode. But applying the changes in play mode is no good neither, as changes are not saved.

How can I create an script that checks if a collider is inside another collide in editor mode, so I can remove it if it does?

Or is there any way to create a script that runs on play mode and use the OnTriggerEnter to delete the collider I want, then save the changes for the editor?

Using bouding boxes is no good, as the collider delimiting the play area is an irregular polygon collider.

Any ideas?

• if you want check something in editor mode you can use [ExecuteInEditMode] and if you want save changes in editor when stop game mode you can use ScriptableObject. – Seyed Morteza Kamali Sep 30 '16 at 10:14
• [ExecuteInEditMode] calls Update, OnGUI, OnRenderObject. Physics are disabled on Edit mode and no collisions will be detected. About pausing and using scriptable object, what do you exactly mean? – Leo Sep 30 '16 at 10:41
• To clarify, you need objects to move, through physics calculations, and only after the movement do you need to start removing the collider component? – eclmist Sep 30 '16 at 11:45
• I don't need to move anything. I need to know which colliders are inside one specific polygon collider, and emove those that are inside the polygon collider. – Leo Sep 30 '16 at 11:50
• If this is for compile time, what is stopping you from manually deleting the objects? Also, what is stopping you from doing this on scene load, during runtime? – eclmist Sep 30 '16 at 12:01

There's a little hack that I used to do in order to save runtime object states. You simply create a prefab during runtime.

Presumably, you are only doing this in code because there is a large number of gameobjects. I will assume these objects are all childs of one parent object.

Next, run the game with a simple script that removes colliders from the obstacles in this collection of obstacles.

Then, in play mode, simply drag this parent Obstacles object into your project tab creating a prefab.

And voilà! You now have a prefab with colliders removed from certain objects.

Now go back to editor mode and replace the Obstacles object with your newly created runtime Obstacles object.

• Didn't know you could save prefabs during runtime. Amazing! Thanks! – Leo Sep 30 '16 at 15:38