Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Rigidbody2D.bodyType not working

Discussion in 'Physics' started by Juli2307, Aug 2, 2020.

  1. Juli2307

    Juli2307

    Joined:
    Apr 13, 2019
    Posts:
    9
    Hi, I'm Julian. I have a problem changing body types via script. The thing is that when I try to change it from static to dynamic, and then back to static, it stays at dynamic.
    Here's that code:
    Code (CSharp):
    1. public class Ball : MonoBehaviour
    2. {
    3.     [HideInInspector] public bool playing;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         playing = false;
    9.         Physics2D.gravity = new Vector2(0, -9.8f);
    10.         this.gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
    11.     }
    12.  
    13.     public void play()
    14.     {
    15.         playing = true;
    16.         this.gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    17.     }
    18.  
    19.     public void reload()
    20.     {
    21.         playing = false;
    22.         gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
    23.     }
    24.  
    25.     public void BackToMenu()
    26.     {
    27.         SceneManager.LoadScene("Level Selector", LoadSceneMode.Single);
    28.     }
    29. }
    I don't have any idea if I'm doing something wrong or if I'm missing something.
    Please help.
    -Julian
     
    Last edited: Aug 3, 2020
  2. Juli2307

    Juli2307

    Joined:
    Apr 13, 2019
    Posts:
    9
    I am dumb:
    Pic.png
    I was calling the "reload" function on the prefab of the ball, not on the scene object.