How do I change a Physics Material through a Script?

I’m not all that good at coding at the moment, and I’m trying to change an object’s Physics Material through a script. I can’t figure it out. Can someone help?

If what you need is to change the properties of the material through code, you can do that by accessing the collider’s .material property as documented:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float dynFriction;
    public float statFriction;
    public Collider coll;
    void Start() {
        coll = GetComponent<Collider>();
        coll.material.dynamicFriction = dynFriction;
        coll.material.staticFriction = statFriction;
    }
}

If however what you need is to change the material with another pre-made one, simply assign it’s reference to your collider, replacing it’s current material.

public PhysicMaterial physicMaterial;

public void OnSomething()
{
    GetComponent<Collider>().material = physicMaterial;
}

Is this what you needed?