- Home /
Set Rigidbody variable to Prefab using code
I'm looking to set a Rigidbody variable, using code instead of the Inspector, to a Prefab which already exists in the Unity Project. I tried using GetComponent() but the Prefab is not returned. Is there a way to do this, or am I forced to create the Prefeb in code first?
Answer by Bunny83 · Sep 28, 2011 at 05:31 PM
You have to load the prefab asset to be able to assign it's reference to a variable. Use AssetDatabase.LoadAssetAtPath to load your prefab from your asset-folder and then you need to get the reference to the object on which you want to set the variable.
Note that this is all stuff form the UnityEditor namespace and you can only use the assetdatabase in the editor. Even prefabs are an editor-only feature (at least until now).
Your answer
Follow this Question
Related Questions
Prefab (Project Folder) to a GameObject (on the Scene) 2 Answers
Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers
How can i access in a prefab a variable from a gameObject? 1 Answer
How do I assign a prefab reference to a private variable? 1 Answer
Setting a game object from a list to be the active character that gets instantiated 1 Answer