- Home /
Changing physics material at runtime not working
Here's the relevant snippet of my code:
var bounce = PlayerPrefs.GetInt("ExtraBounce");
var setBounce = collider.material.bounciness;
switch(bounce){
case 1: setBounce = 0.1;break;
case 2: setBounce = 0.2;break;
case 3: setBounce = 0.3;break;
case 4: setBounce = 0.4;break;}
I want to change the bounciness of this object based on the player preferences.
Thanks for the help,
Adam
So no help on this one? This doesn't happen to be with OnCollisionEnter, does it?
Answer by FirePlantGames · Apr 01, 2014 at 03:36 AM
sorry, this is REALLY LATE:
function Update()
{
//if you left click the mouse the bounce will increase
if (Input.GetButtonDown("Fire1"))
collider.material.bounciness += 1;
//if you right click the mouse you will decrease
if (Input.GetButtonDown("Fire2"))
collider.material.bounciness -= 1;
}
Answer by Bunny83 · Apr 01, 2014 at 03:23 AM
Uhm, "bounciness" is a float. When you store it in a local variable like you did it's just a copy of that value. Setting this copy to a different value won't change "collider.material.bounciness". You have to assign it to bounciness.
var mat = collider.material;
switch(bounce)
{
case 1: mat.bounciness = 0.1; break;
case 2: mat.bounciness = 0.2; break;
case 3: mat.bounciness = 0.3; break;
case 4: mat.bounciness = 0.4; break;
}
It seems a lot people aren't familiar with the absolut basic syntax rules.
The assignment operator ( = ) does two things:
It reads the value of whatever is on the right side
and assigns this value to whatever is on the left side.
The value which is assigned is always copied. So in the OPs code snippet "setBounce" gets a copy of the current float value that's stored in bounciness. The copy in setBounce has no relation to the Physic$$anonymous$$aterial anymore. It's just a float value. So changing this float value to something else does only change the local variable "setBounce".
In my solution i store the physicmaterial of the collider in a local variable mat. Again the assignment operator always just copies the value. The big difference now is a Physic$$anonymous$$aterial is not a value-type like a float / int / ... it's a reference type. So the value of collider.material is a reference (some kind of pointer) to a Physic$$anonymous$$aterial instance. Once copied into the variable "mat" both the local variable "mat" as well as collider.material contain the same value, the same reference to the Physic$$anonymous$$aterial.
Since both represent the same object those two lines are identically:
collider.material.bounciness = 5;
mat.bounciness = 5;
So you can use the mat variable to access (read the reference and use it) the Physic$$anonymous$$aterial which is referenced and then assign a new value to the bounciness field.
What doesn't work, like in the questions code, you can't change the whole material that way.
var mat = collider.material;
mat = someOtherPhysic$$anonymous$$aterial;
This would just replace the reference stored in "mat" with the reference stored in "someOtherPhysic$$anonymous$$aterial" but won't change "collider.material". To exchange the material you have to assign it directly to "collider.material":
collider.material = someOtherPhysic$$anonymous$$aterial;
I was trying to change the bounciness of my material which for some reason wasn't working then i saw this. why not change the material to one which has the desired bounciness basically. easy as pie to do and works. you win the internet today.
Your answer
Follow this Question
Related Questions
Bouncy rigid body, although bounciness is 0? 1 Answer
PhysicsMaterial2D and bounciness: Getting weird angles when hitting box collider edges 2 Answers
Export objects to a .3DS file at runtime 1 Answer
Predicting projectile path after bouncing off Physics Material 2D 1 Answer
Lens Flares shines through (maya)-object geometry.. 1 Answer