Add Rigidbody in Unity with Scripts

You can add a Rigidbody through scripting in Unity by following these steps:

  1. Create an empty GameObject and give it the name you desire.
  2. Create a C# script and name it whatever you’d like.
  3. Double-click to open the script and add the following code to the script.
using UnityEngine;

public class AddRigidbody : MonoBehaviour
{
    private void Start()
    {
        // 添加刚体组件
        Rigidbody rb = gameObject.AddComponent<Rigidbody>();
    }
}
  1. Add the script component to the GameObject.
  2. When running the game, the Rigidbody component will be added to this GameObject.

In this way, you can add a Rigidbody through the script.

bannerAds