Change bounciness in a 2D game

Hello!
I am trying to change the bounciness value in a game object through code.
I have my main character and I have attached a circle collider to him. In the circle collider I have added a physics material so he can be bouncy. I changed the bounciness value to 0.5 and I want for this value to change to zero when the player hits the W button.

void Start {
        Bouncy = GetComponent<CircleCollider2D>();
}

void Update {
        if (Input.GetButtonDown("Crouch")) {
                crouch = true;
                [here is where i want to change the bounciness value from 0.5 
                 to 0, so he can stop bouncing]
        }
}

Its a known bug from Unity 5.1, still present in 2019.3.7.
Here is a work around, simply toggle the collider:

        public float Bounce
        {
            get { return collider2d.bounciness; }
            set
            {
                collider2d.enabled = false;
                collider2d.sharedMaterial.bounciness = value;
                collider2d.enabled = true;
            }
        }

What you need are two physics materials, one that has the bounce and one where bounce is zero.

When I’ve done this kind of thing, I prefer to make the materials in code, but there is a way to load the material from resources if you prefer (and I’ll let you look that part up if you do).

Assume there are these two members declared in your class:

public PhysicMaterial  bouncy;
public PhysicMaterial  nobounce;

In many cases I need these for a large number of this same object (lots of balls in the arena, lets say), so when I do, I make these static. If they are static, I include a static bool defaulted to false to indicate initialization of the physics materials, so that is only done once.

To initialize these materials, do something like the following in Start (or some initialization code):

bouncy = new PhysicMaterial();

bouncy.dynamicFriction = 0.4f; // or whatever friction you like
bouncy.staticFriction = 0.5f; // as above
bouncy.bounciness = 0.8f; // 
bouncy.frictionCombine = PhysicMaterialCombine.Average; // the typical default
bouncy.bounceCombine = PhysicMaterialCombine.Average;

Repeat for nobounce, but make bounciness 0 for that version, and probably best to use PhysicMaterialCombine.minimum for the bounceCombine parameter (experiment).

Now, you need to keep in mind that this is only part of the bounce equation. If you still have some bounce, you may need to do this for the material(s) the ball bounces against, too.

Now, at the moment you need to disable bounce, do something like:

SphereCollider sc = GetComponent< SphereCollider >() as SphereCollider;
sc.material = nobounce;

Substitute whatever collider component your using, and consider doing it for the material the ball is bouncing against (with whatever collider that is using).

To enable, repeat that with “bounce” as the physics material.

@ Reydion

PhysicsMaterial2D ball_bounciness = new PhysicsMaterial2D(“bounce”);
ball_bounciness.bounciness = bounce;
ball_bounciness.friction = 0.4f;

//change PhysicsMaterial2D of ball GameObject
gameObject.GetComponent().sharedMaterial = ball_bounciness;