- Home /
Unable to access material property of PhysicsMaterial2D dynamically in unity2D
So I have a script that dynamically creates colliders for the edges of the screen. Now I want to make them bouncy.
I try to do this:
gameObject.GetComponent<Collider2D>().material.bounciness = 1;
but it says UnityEngine has no definition for material.
If I replace the Collider2D with just Collider, the material property is accessible. But since my game is 2D, I need it to work with Collider2D.
How do I do it? If it's not possible, what are the workarounds?
Answer by SkaredCreations · Sep 07, 2018 at 01:44 PM
Collider2D has no property called "material", but it has a property called "bounciness" (and also a property "sharedMaterial" that is a reference to a PhysicsMaterial2D which also has the property "bounciness"). May be that the bounciness property of Collider2D is a shortcut for sharedMaterial.bounciness, I never tried to use this so I'm not 100% sure.
I tried that one but for some reason the character did not bounce after setting bounciness or shared$$anonymous$$aterial.bounciness to 1
Your answer
Follow this Question
Related Questions
Why Collider2D.OverlapPoint does not work with CompositeCollider2D? 1 Answer
My OnCollisionEnter says i have 128 Collisions every collision 1 Answer
Physics2D.OverlapCircleAll and OverlapCapsuleAll behaves differently? 0 Answers
problem solving ghost vertices with trigger and collision colliders 0 Answers
Unity 2D game: all colliders not working 3 Answers