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 hamstar · Nov 20, 2013 at 05:12 PM · physicscollider2d-physicsphysicmaterial

Change a Collider 2D physics material at runtime?

If I change the physics material of a BoxCollider2D or EdgeCollider2D at runtime, it seems that the previous material is still used. I haven't tested other colliders yet. The Inspector shows the correct material assigned (new one).

Test script:

 public class AssignPhysicsMaterial : MonoBehaviour {
     public PhysicsMaterial2D mat;
 
     void Start() {
         collider2D.sharedMaterial = mat;
     }
 }

For example, I created a PhysicsMaterial2D with Bounciness "1". If I assign this material in the inspector before playing, the ball bounces continuously as expected. If I assign it at runtime through code, the ball doesn't bounce, as if the default physics material is still being used (although inspector shows otherwise).

Is this a bug or have I missed something?

Comment
Add comment · Show 6
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 Tomer-Barkan · Nov 20, 2013 at 06:02 PM 0
Share

If you set the material in the inspector, not in code, does it work as you expect? $$anonymous$$aybe it's something to do with the physics material of the floor.

avatar image hamstar · Nov 20, 2013 at 06:04 PM 0
Share

It works if I set it before hitting play. It doesn't work if I set the material while playing, manually with the inspector or with code.

avatar image Tomer-Barkan · Nov 21, 2013 at 05:28 AM 0
Share

That sounds weird... Did you try to see if with 3d colliders it works as expected? If it does, it might be a bug in the new 2d engine.

avatar image Eric5h5 · Nov 21, 2013 at 06:11 AM 0
Share

I'd say it's a bug.

avatar image hamstar · Nov 21, 2013 at 10:36 AM 0
Share

I just tested again and actually it does seem to work if I set the material in the inspector, but ONLY if it has not already been changed by a script... odd. I'll report it as a bug.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hamstar · Nov 25, 2013 at 04:53 PM

I emailed Unity support who agreed that this looks like a bug. I submitted a bug report and it's been accepted.

The example project I submitted can be downloaded here.

[1]: http://fogbugz.unity3d.com/default.asp?577228_no7i4p502369okao

Comment
Add comment · Show 4 · 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 hamstar · Dec 03, 2013 at 10:23 AM 1
Share

There's a work around which was posted here. If you change the physics material whilst the collider is disabled, and then enable it, the changes are applied.

avatar image LaSuttSutt · Oct 14, 2014 at 08:42 AM 0
Share

The problem with this work around is that there are side effects in cause of disable/enable the collider. In my case i've had another collider as trigger. That collider should triggering but it does not if i disable/enable the other collider for changing the material. I hope this bug gets fixed soon...

avatar image otomo · Jun 11, 2015 at 03:45 PM 0
Share

stlll not fixed

avatar image Entercyber · Jun 12, 2015 at 09:27 AM 0
Share

yep i got this problem now, shame thay have not fixed it yet...

avatar image
0

Answer by MelvMay · Jul 13, 2015 at 04:47 PM

This was fixed in 5.1.1p2 on the 26th June 2015.

Comment
Add comment · Show 10 · 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 jniac · Oct 06, 2015 at 05:15 PM 1
Share

Really ? So it was then successfully unfixed in 5.2.

avatar image MelvMay ♦♦ jniac · Oct 07, 2015 at 08:13 AM 0
Share

Snidey remark aside, this case is still fixed in 5.2.0f3 yes.

avatar image instruct9r MelvMay ♦♦ · Oct 11, 2015 at 12:09 PM 0
Share

5.2.1f1, this still doesn't work. Actually.

Changing it in the editor while in Playmode, doesn't affect the Rigidbodies's friction. If i disable \ enable the collider it get's updated though.

So i guess it's still NOT fixed.

This is aboud 2D. Not sure if in 3D mode it works...

Show more comments
avatar image hawken · Jan 10, 2016 at 05:03 AM 0
Share

O$$anonymous$$ I just ran in to this bug and can confirm it still exists in 5.3.1p1

You can't change material in run time (material changes but previous materials friction and bouncyness are unchanged)

avatar image FelixTwoMenAndADog hawken · Jun 30, 2016 at 12:51 PM 0
Share

Still broken in 5.3.5p4.

Works (as above) when deactivating and activating colliders.

avatar image justaddice83 · Apr 14, 2020 at 02:01 PM 0
Share

Still present in Unity 2019.3.7. I had to use the work around.

avatar image
0

Answer by hawken · Aug 11, 2017 at 12:14 PM

Just tried this in Unity 5.6.2 and can confirm the bug still exists, @MelvMay using the following type of code, the material in the inspector changes but the previous material's bouncy / friction are left in use.

 void ChangeMat()
 {
     if (useMat2)
     { 
         rb.sharedMaterial = Resources.Load("Materials/Physics/mat2") as PhysicsMaterial2D;
     }
     else
     {
         rb.sharedMaterial = Resources.Load("Materials/Physics/mat1") as PhysicsMaterial2D;
     }
 }

adding this code at the end works (warning, this crashes Unity like crazy):

 rb.simulated = false;
 rb.simulated = true;

I found that turning the collider on and off as suggested works well, using the following code:

 cl.enabled = false;
 cl.enabled = true;

The only problem with doing things like this is that if you happen to be in a 1 way collider and half or more of the collider you are enabling is in any of the 1way, your collider will jump to the top of the surface arc, something to bare in mind.

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 MelvMay ♦♦ · Aug 14, 2017 at 10:45 AM 0
Share

Well I don't understand why as I'm looking at the code and the assignment takes place fine. $$anonymous$$now that this only affects new contacts, not existing ones. Disabling/simulation-off then on causes contacts to be recalculated so maybe that's what you're seeing.

Also, I don't follow your note of "warning, this crashes Unity like crazy"; I've not seen any reports of turning simulation off then on causing a crash. If that's true then please can you report a bug for it?

avatar image hawken MelvMay ♦♦ · Aug 15, 2017 at 01:56 AM 0
Share

That might be where the perceived "bug" comes from regarding a game design point of view, for example if you have a player on a slope with "climbing shoes", when that ability is taken away from a player (climbing shoes are swapped for greased socks), it would be expected by doing a material change the player should then slide down the slope.

Turning the collider off / on certainly works but might not be desirable in a game due to unpredictable side effects (re-triggering trigger, popping to top of 1-ways etc).

I'll submit a bug report for the crash! :)

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity physics - how to make an object with a box collider and a rigidbody slide across a slightly undulating surface 0 Answers

Detect bounce amount 1 Answer

Raycast 2D with origin inside the collider 1 Answer

Slant Physics Raycast Implementation 0 Answers

Do you have to have a rigidbody component for collision detection? 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