The Object you want to instantiate is null.

Hello,

I am trying to instantiate a prefab from resources folder using the name from a text file located in my resources folder. Prefab is also located in my resources folder. However, I get an error saying “the object I want to instantiate is null”.

When I directly give the name of prefab it works. I cannot seem to understand the issue.

My code is :

public void Loadd()
{

	TextAsset txtasset = (TextAsset)Resources.Load (txtfile);
	txtcontents = txtasset.text;
	string[] linesinfile = txtasset.text.Split ('

');

	for(int i= 0; i<1; i++){

		for (int x = 0; x < buttonholder.Length; x++) {
			//GameObject Instance = Instantiate (Resources.Load ("F260001", typeof(GameObject))) as GameObject;   // works

			string name = linesinfile [x];
			Debug.Log (name);

			GameObject Instance = Instantiate (Resources.Load (name, typeof(GameObject))) as GameObject;

		}
	}
}

If linesinfile [0] == “F260001” is false and yet they look the same in the console then there is something different about the strings that is not showing up in console. As @hexagonius suggested that might be a space; however you checked for that. You split the file string on
but some text editors add
when you press return.
So the string you thought looked like this:
F26001
F26002
F26003
Really looks like this:
F26001
F26002
F26003

Try using a different text editor (and checking the invisible characters) or set up your textfile with a different separator; e.g.: F26001;F26002;F26003
Then use: txtasset.text.Split (‘;’);