0
\$\begingroup\$

How could I put a prefab into a field of a game object using editor API in Unity?

I have a game object. It has a GameObject field. I can drag and drop a prefab from my assets into that field.

Now I want to take the prefab (I have a path to it in my code) and put it into that game object field using editor API. Is it possible somehow?

I need this to automate some of my scene setup.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can just assign the value in code.

myGameObject = thingToAssign;

You may want to do this in OnValidate or with ExecuteInEditMode.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Yes, that worked. Also, I am creating a prefab using this api: PrefabUtility.SaveAsPrefabAssetAndConnect, so I just assign the result returned by the SaveAsPrefabAssetAndConnect. \$\endgroup\$
    – qqqqqqq
    Jun 17, 2021 at 18:32
  • \$\begingroup\$ I got a strange behavior. Whenever I assign a property through the editor API, that property gets cleared on Play. Do you know any reasons for that? I use menu item to run an editor API code. \$\endgroup\$
    – qqqqqqq
    Jun 18, 2021 at 19:19
  • \$\begingroup\$ It could be 100 different things. Nothing particularly stands out to me. Can you ask a new question and provide as much information as you can? \$\endgroup\$
    – Evorlor
    Jun 18, 2021 at 19:29
  • 1
    \$\begingroup\$ Done here. \$\endgroup\$
    – qqqqqqq
    Jun 18, 2021 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .