ArgumentException: The prefab you want to instantiate is null.
I get the above error when implementing the following code. I have cut it down to what I think are the essential bits of code to make it easier to read. If you need more please let me know.
public GameObject WolfObject;//prefab has been dragged into this object
public GameObject SpeciesInstantiate(string SpeciesName,int IDNameNumber)
{
var NewGameObject=GameObject.Instantiate(WolfObject,Position,Quaternion.identity);
NewGameObject.name=(SpeciesName+IDNameNumber.ToString());return (GameObject)NewGameObject;}
}
That is called as follows:
public class EnvironmentalEngine : MonoBehaviour
{
GameObject ComObj;
void Start ()
{
ComObj=GameSpecies.SpeciesInstantiate(SpeciesTag[j],ObjectLibrary.WorldSpeciesCount[j]); }
Thanks in advance for any help :)
Perhaps the prefab got changed externally, like rena$$anonymous$$g in windows explorer, thus breaking the prefab connection? I really am guessing but just maybe re-assign the prefab? Probably won't help :p
In my experience, Unity functions in C# can be a lot more fussy about types at times when it might seem to make less sense to do so. Have you tried casting WolfObject to Object? Instantiate takes Object as its spawn argument and it may be having issues converting the type automagically.
Tried casting it to UnityEngine.Object. That didnt work. I appreciate the help though :)
Have you tried Googling for 'The prefab you want to instantiate is null Unity3'? You will find some hits on UnityAnswers. $$anonymous$$any are uninitialized game objects, but there were several where the problem was more subtle. With the limited amount of code you gave here, it is unclear which (if any) of the more subtle issues might apply to your problem.
Answer by RoloJo · Mar 13, 2013 at 04:52 PM
Thanks for all the responses. After trawling the net I have found what makes it work:
Take the prefab that you want to Instantiate and put it in a folder (you will have to create it in the project folder) called "Resources". Place the prefabs that you wish to Instantiate in there. In this case the prefab is called "Wolf".
Then change this line:
var NewGameObject=GameObject.Instantiate(WolfObject,Position,Quaternion.identity);
to:
var NewGameObject=GameObject.Instantiate(Resource.Load("Wolf"),Position,Quaternion.identity);
And now it works! :)
Was your prefab not already in the 'Assets' folder? Your original code should work if your prefab was anywhere in the Assets folder.
Ah, ok that must be it. The prefab was in the project folder.
Cheers :)
Erm... The project folder contains all the assets? That's strange - it should have worked before if all you did was move it. Well hey it works now, good luck!
I have a problem similar to this one except I cannot instantiate the WWW-delivered asset bundle, but I can instantiate the local prefab (as loaded from the Resources directory) that it is based on. It gives me the same "ArgumentException: The prefab you want to instantiate is null" error. As others have mentioned, I too thought your original code should've worked if it was properly connected in the Inspector.
Answer by colorpillar · Oct 15, 2015 at 06:13 PM
Hello, if you wish to organise your prefabs in a subfolder of Resources you must first make a folder named "Prefabs" within Resources. Move your subfolder there.
In Javascript you can then load thusly: var NewGameObject : GameObject = Resources.Load("Prefabs/Your SubFolder/Your Prefab"),Vector3.zero,Quaternion.identity);
You must also remove the ".prefab" extension from your FileName string. ,