If you select a script in the Project pane, for example this test2.cs, you can set any public variables. In this example I assigned a public GameObject to a prefab.
However when I call AddComponent(), adding test2 to a new GameObject, and the test2 Awake() function is run, its m_prefab member variable is null.
What’s the point in displaying them this way? How can I set them? Ideally I want to set them in a static-like way, but if I actually make them static, they don’t show up in the editor.
What am I doing wrong?
Assign the Prefab variable from code just after you add the component:
test2 _test2 = AddComponent<test2>();
_test2.Prefab = GameObject.Find("Cube");
There are a couple of different ways to get your variable initialized. First, a prefab doesn’t have to be a true prefab from the Project pane. You can use any scene game object. You have to have to disable colliders and renderers and re-enable them on the clone object. If you are using a Scene item, you can GameObject.Find() or GameObject.FindWithTag() or any of the other methods of find games object and initialize your m_prefab variable.
If you want a true prefab, then you can put your prefab in the Assets/Resources folder and use Resources.Load() to initialize your variable:
void Start ()
m_prefab = Resources.Load("prefabName") as GameObject;
If you are not going to be Instantiating a lot of your prefabs, you can skip the initialization of m_prefab and just do:
GameObject instance = Instantiate(Resources.Load("prefabName")) as GameObject;