The thing you want to instantiate is null.

Really confused here. So I try to instantiate a prefab from resources and everything works fine :slight_smile:
But if I save the String where the resource is in a txt, then retrieve it I get the error message “The thing you want to instantiate is null.” .
Heres the code:
Debug.Log( prefabnames[j]);
prefabnames[0]=“Prefabs/Plank1”;
var newplank: GameObject = Instantiate(Resources.Load(prefabnames[j]),Tp2,Tr2);

I put in prefabnames[0]=“Prefabs/Plank1”; and so when j=0 it works, if I dont have prefabnames[0]=“Prefabs/Plank1”; it will not work. the Debug.Log reads “Prefabs/Plank1” without the quotes.
Any Ideas? It works fine when I dont load prefabnames[ ] from a text file.

Think I’ve figured it out, the one from the txt has a length of 15 for some reason :s (should be 14) now to delete this mystery character :slight_smile:

Edit: Just in case anyone else has this problem this is the fix I used after reading from the txt do this:
prefabnames[j]=prefabnames[j].Remove(prefabnames[j].Length-1);

1 Like

The issue is from Resources.Load returning null. you probably fixed this by removing the newline character at the end. To avoid the assertion though you can change your code to check if Resources.Load returns something existent before instantiating.

Just deleting the last character whatever it is seems…wrong…
If you’re only storing prefabnames in your text file you should write them each in a new line and use the split function to load it into an array:

//Just an Example
string input = "Prefab1/nPrefab2/nPrefab3/n";
string[] prefabs = input.Split('/n');
foreach(string prefab in prefabs)
    if(prefab != "")
        Instantiate(...);

If you’re storing more than just the name of the Prefab your Instantiating (like position, rotation, name…) in the text file you should think about XML-Serialization.

Each prefab was on a newline, it was adding some mystery value to the end in windows, but when I ran it on the android it worked fine (well after I removed the dodgy line of code). Maybe it was because the editor was running in android mode?

I’ve been dealing with the same issue. Thanks for the solution! :slight_smile: