- Home /
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?
Answer by Ramlock · Mar 26, 2018 at 03:45 AM
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: https://docs.unity3d.com/ScriptReference/Collider.html https://docs.unity3d.com/ScriptReference/Collider-material.html https://docs.unity3d.com/ScriptReference/PhysicMaterial.html
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?
I've tried both of these solutions. I have two game object one of which is placed on the other with an angle to the horizontal plane. The materials are set correctly, i.e. both dynamic and static frictions are set to 0.0F for both of the objects. Since gravity is applied, I expect the upper one to slide downwards. But there is no movement. Why? I don't get it...
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer