- Home /
Change a Collider 2D physics material at runtime?
If I change the physics material of a BoxCollider2D or EdgeCollider2D at runtime, it seems that the previous material is still used. I haven't tested other colliders yet. The Inspector shows the correct material assigned (new one).
Test script:
public class AssignPhysicsMaterial : MonoBehaviour {
public PhysicsMaterial2D mat;
void Start() {
collider2D.sharedMaterial = mat;
}
}
For example, I created a PhysicsMaterial2D with Bounciness "1". If I assign this material in the inspector before playing, the ball bounces continuously as expected. If I assign it at runtime through code, the ball doesn't bounce, as if the default physics material is still being used (although inspector shows otherwise).
Is this a bug or have I missed something?
If you set the material in the inspector, not in code, does it work as you expect? $$anonymous$$aybe it's something to do with the physics material of the floor.
It works if I set it before hitting play. It doesn't work if I set the material while playing, manually with the inspector or with code.
That sounds weird... Did you try to see if with 3d colliders it works as expected? If it does, it might be a bug in the new 2d engine.
I just tested again and actually it does seem to work if I set the material in the inspector, but ONLY if it has not already been changed by a script... odd. I'll report it as a bug.
Answer by hamstar · Nov 25, 2013 at 04:53 PM
I emailed Unity support who agreed that this looks like a bug. I submitted a bug report and it's been accepted.
The example project I submitted can be downloaded here.
[1]: http://fogbugz.unity3d.com/default.asp?577228_no7i4p502369okao
There's a work around which was posted here. If you change the physics material whilst the collider is disabled, and then enable it, the changes are applied.
The problem with this work around is that there are side effects in cause of disable/enable the collider. In my case i've had another collider as trigger. That collider should triggering but it does not if i disable/enable the other collider for changing the material. I hope this bug gets fixed soon...
yep i got this problem now, shame thay have not fixed it yet...
Answer by MelvMay · Jul 13, 2015 at 04:47 PM
This was fixed in 5.1.1p2 on the 26th June 2015.
Snidey remark aside, this case is still fixed in 5.2.0f3 yes.
5.2.1f1, this still doesn't work. Actually.
Changing it in the editor while in Playmode, doesn't affect the Rigidbodies's friction. If i disable \ enable the collider it get's updated though.
So i guess it's still NOT fixed.
This is aboud 2D. Not sure if in 3D mode it works...
O$$anonymous$$ I just ran in to this bug and can confirm it still exists in 5.3.1p1
You can't change material in run time (material changes but previous materials friction and bouncyness are unchanged)
Still broken in 5.3.5p4.
Works (as above) when deactivating and activating colliders.
Still present in Unity 2019.3.7. I had to use the work around.
Answer by hawken · Aug 11, 2017 at 12:14 PM
Just tried this in Unity 5.6.2 and can confirm the bug still exists, @MelvMay using the following type of code, the material in the inspector changes but the previous material's bouncy / friction are left in use.
void ChangeMat()
{
if (useMat2)
{
rb.sharedMaterial = Resources.Load("Materials/Physics/mat2") as PhysicsMaterial2D;
}
else
{
rb.sharedMaterial = Resources.Load("Materials/Physics/mat1") as PhysicsMaterial2D;
}
}
adding this code at the end works (warning, this crashes Unity like crazy):
rb.simulated = false;
rb.simulated = true;
I found that turning the collider on and off as suggested works well, using the following code:
cl.enabled = false;
cl.enabled = true;
The only problem with doing things like this is that if you happen to be in a 1 way collider and half or more of the collider you are enabling is in any of the 1way, your collider will jump to the top of the surface arc, something to bare in mind.
Well I don't understand why as I'm looking at the code and the assignment takes place fine. $$anonymous$$now that this only affects new contacts, not existing ones. Disabling/simulation-off then on causes contacts to be recalculated so maybe that's what you're seeing.
Also, I don't follow your note of "warning, this crashes Unity like crazy"; I've not seen any reports of turning simulation off then on causing a crash. If that's true then please can you report a bug for it?
That might be where the perceived "bug" comes from regarding a game design point of view, for example if you have a player on a slope with "climbing shoes", when that ability is taken away from a player (climbing shoes are swapped for greased socks), it would be expected by doing a material change the player should then slide down the slope.
Turning the collider off / on certainly works but might not be desirable in a game due to unpredictable side effects (re-triggering trigger, popping to top of 1-ways etc).
I'll submit a bug report for the crash! :)