Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Script that changes bounciness and friction of a rigidbody2d only works the second time I play scene

Discussion in 'Physics' started by ohyesplease, Dec 7, 2022.

  1. ohyesplease

    ohyesplease

    Joined:
    Feb 19, 2018
    Posts:
    13
    I have a scene with a purple Capsule with a rigidbody2d which has a PhysicsMaterial2D that I have set up in Assets.
    The Capsule also has a script which contains, e.g.

    public class ChoosePhysicsMaterial2D : MonoBehaviour
    {
    private Rigidbody2D rb2D;
    // Start is called before the first frame update
    void Start()
    {
    rb2D = this.gameObject.GetComponent<Rigidbody2D>();
    rb2D.sharedMaterial.bounciness = 0.0f;
    rb2D.sharedMaterial.friction = 0.9f;
    Debug.Log("Purple bounciness is: " + this.gameObject.GetComponent<Rigidbody2D>().sharedMaterial.bounciness);
    Debug.Log("Purple friction is: " + this.gameObject.GetComponent<Rigidbody2D>().sharedMaterial.friction);
    }
    }


    I keep experimenting with different values of bounciness and friction and recompiling. When I play the scene in the Editor, the first time, the Debugs show the values I have set, but the object behaves according to the previous settings. The second time I play the scene, the object behaves according to the correct new settings.

    How can I fix this?
     
    Last edited: Dec 7, 2022
  2. ohyesplease

    ohyesplease

    Joined:
    Feb 19, 2018
    Posts:
    13
    Last edited: Dec 8, 2022
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,657
    No there isn't a bug. You need to reassign the material. The 2D system doesn't support dynamically changing materials like this because the underlying 2D physics engine (Box2D) doesn't support it unlike the 3D physics engine (PhysX).

    You don't need to disable/enable it, just reassign the material.
     
  4. ohyesplease

    ohyesplease

    Joined:
    Feb 19, 2018
    Posts:
    13
    Thanks for the info. However it's this phrase "just reassign the material" that's causing all my confusion (that you could see in the other thread that we have been looking at). I thought my code did assign it. I thought my rb2D was a reference to a component of a game object and that my
    rb2D.sharedMaterial.bounciness = 0.0f;
    was setting a value belonging to a thing that belonged to that component.

    Certainly, when my debugs access that value in the referenced component, they find the new value.

    Obviously, I don't really understand C# and referencing, but I can't guess what else I need to do to make the data "stick". Surely I don't just repeat the assignment? Or do I have to create a local struct of type sharedMaterial and assign that to rb2D.sharedMaterial? Any more detailed hints would be welcome.