- Home /
get component on prefab in script C#
hey,
i have this code where I create an object via a prefab, and I need to get the rigidbody component of that prefab
my code is looking kind of like this:
public class prefab : MonoBehaviour
{
[SerializeField] private Transform prefa;
}
Comment
Answer by logicandchaos · Jul 05, 2020 at 06:49 PM
To get the component you use the GetComponent() method. so you would do prefa.GetComponent(); and you cash that by making a Rigidbody variable RidgidBody rb; then in start() or awake() you put: rb = prefa.GetComponent(); then you access your rigidbody with rb
Your answer
![](https://koobas.hobune.stream/wayback/20220613002535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Prefab with component, and attached sub component 1 Answer
Multiple Cars not working 1 Answer
Accessing component of a single prefabs' instance. 0 Answers
get component on prefab in script C# 1 Answer
Distribute terrain in zones 3 Answers