Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by huglojsk · Feb 09, 2014 at 12:48 AM · runtimebouncinessphysicsmaterial

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zafadadynamic · Mar 23, 2014 at 09:49 PM 0
Share

So no help on this one? This doesn't happen to be with OnCollisionEnter, does it?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 01, 2014 at 03:53 AM 1
Share

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;
avatar image Legendmir Bunny83 · Feb 03, 2018 at 12:05 AM 0
Share

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.

alt text

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges