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?

| improve this question | | | | |
  • \$\begingroup\$ 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. \$\endgroup\$ – Seyed Morteza Kamali Sep 30 '16 at 10:14
  • \$\begingroup\$ [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? \$\endgroup\$ – Leo Sep 30 '16 at 10:41
  • \$\begingroup\$ To clarify, you need objects to move, through physics calculations, and only after the movement do you need to start removing the collider component? \$\endgroup\$ – eclmist Sep 30 '16 at 11:45
  • \$\begingroup\$ 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. \$\endgroup\$ – Leo Sep 30 '16 at 11:50
  • \$\begingroup\$ 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? \$\endgroup\$ – 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.

enter image description here

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.

enter image description here

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

enter image description here

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

| improve this answer | | | | |
  • \$\begingroup\$ Didn't know you could save prefabs during runtime. Amazing! Thanks! \$\endgroup\$ – Leo Sep 30 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.