- Home /
Physics Material 2D change during runtime not taking effect.
Hey, I have this gameObject that changes it PhysicsMaterial2D, on it's CircleCollider2D during runtime, but it is still acting as if it doesn't have the Physics Material. I change it like this:
var yellowPMat : PhysicsMaterial2D;
function Update () {
if (color == 'yellow') {
gameObject.GetComponent(CircleCollider2D).sharedMaterial = yellowPMat;
}
else {
gameObject.GetComponent(CircleCollider2D).sharedMaterial = null;
}
}
I can see in the editor that the material on the CircleCollider2D is changing, but it is not having any effect on the ball's bounciness (the bounciness in the PMat is 0.9) so I don't know what's happening. I feel like I am missing something, so someone help me please. :P Thanks. :D
Answer by Jtec · Nov 21, 2013 at 03:15 AM
I fixed it myself, but I am still unaware why the issue was occuring in the first place. To fix it I quickly flicked the collider on and off again.
var yellowPMat : PhysicsMaterial2D;
if (color == 'yellow') {
gameObject.collider2D.sharedMaterial = yellowPMat;
gameObject.collider2D.enabled = false;
gameObject.collider2D.enabled = true;
}
else {
gameObject.collider2D.sharedMaterial = null;
gameObject.collider2D.enabled = false;
gameObject.collider2D.enabled = true;
}
So yeah, flicking it off and on again fixed my problem. :P
This fix didn't work me unfortunately! Although I'm using EdgeCollider2D(s), not circle colliders. Just so you know, I had the same issue and submitted a bug report which Unity accepted. So you weren't doing anything wrong.
Yeah, I feel like my fix wasn't a REAL fix as such, it just seemed like a dodgy way to get it to work, but it did the job in my case anyway. :P
I must have made a mistake before because I tested it again and it did work (disabling > setting material > enabling). Thanks for the tip.
It works for that single collider but be careful with other colliders. If you have trigger colliders which collides with that enable/disable collider in this second, the trigger won't work.
This is still a problem in Unity 5 and this solution still works.