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?
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.
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.
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...
OK 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)
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. Know 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?
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.
– hamstarI'd say it's a bug.
– Eric5h5I 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.
– hamstar