How to make reference from prefab to variable via C#/JavaScript?

Dear Unity,

I was looking for a way to make reference from Prefab to variable of type GameObject.

Inside the editor, it is no problem at all. Just drag and drop a prefab on to public variable, of type GameObject, inside your GameObject`s script.

But, queastion is, how to perform this “drag and drop” via script?

Example:

We know name of prefab lets say “Soldier”.

We have script like this. It will instantiate GameObject from variable ObjectTest.

using UnityEngine;
using System.Collections;
	
public class TEST : MonoBehaviour 
{
	public GameObject ObjectTest;
	
	void Start ()
	{
		Instantiate(ObjectTest);
	}
}

This will work only if “Soldier” prefab is referenced to variable in editor by drug and drop it. just like in this official Unity tutorial http://www.youtube.com/watch?v=4rZAAHevq1s&list=PLX2vGYjWbI0RmYmP19OimzWSy2PpLxMk7&index=23

While I do understand, I can create an empty GameObject with reference to Prefab, and then use it each time instantiate needed. But still… I do want to know how to do it by script.

Thank you for reading and hopefully answering. Have a nice day!

Dear syclamoth,

Thank you for pointing me this simple thing.

So, drag and droping actually importing prefab from Assets to build, and thus prefab can be accessed from inside build it self. In other words all prefabs in Assets folder, do not go to build automatically, and thus they are not part of GameObjects collection and thus cant be found by GameObject.find(); method.

By syclamoth as an answer,

That is correct. If this were not the case, you could easily find yourself enormous builds filled with junk data, especially if you are keeping deprecated assets around.