- Home /
Question by
Spukyonn · Oct 16, 2017 at 12:43 PM ·
collisioncollideroncollisionentersharedmaterial
Collision audio for different surfaces
So I'm trying to make it so when the player comes into contact with a surface it plays a specific sound depending on the surface. I'm currently using this code:
private void OnCollisionEnter (Collision collision)
{
switch (collision.collider.sharedMaterial.name)
{
case "Wood": //Play wood sound
case "Metal": //Play metal sound
case "Ice": //Play ice sound
}
}
The sound plays but the console gives me the warning "Object reference not set to an instance of an object" when the player touches a surface that doesn't have a physics material. Should I use tags to do this instead of Collision.sharedMaterial? Also, what's the difference between Collision.sharedMaterial and Collision.material?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612141152im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollisionEnter 1 Answer
Unity3d Arkanoid platform collision problem 1 Answer
Character controller collision 2 Answers
How to get expanding object collide when it touches others? 1 Answer
2 objects collide, need to destroy one 2 Answers