Add Rigidbody in Unity with Scripts
You can add a Rigidbody through scripting in Unity by following these steps:
- Create an empty GameObject and give it the name you desire.
- Create a C# script and name it whatever you’d like.
- 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>();
}
}
- Add the script component to the GameObject.
- When running the game, the Rigidbody component will be added to this GameObject.
In this way, you can add a Rigidbody through the script.