I am trying to copy data over from a binary file but when trying to instantiate a gameobject, it comes out as "null".

static void LoadBuildings(SaveBuilding[] buildings)
        if (buildings.Length == 0)
        foreach(SaveBuilding b in buildings)
            if(b.type.ToString() == "Farm")
                Farm build = Instantiate(Resources.Load("Farm")) as Farm;
                Debug.Log("Build: " + build);
                build.position.x = b.positionX;
                build.position.y = b.positionY;
                build.level = b.level;
                build.levelText = build.GetComponentInChildren<Text>();

The debug line shows there is no object and when the next line tries to execute I get an error ArgumentException: The Object you want to instantiate is null.

I initially had it as Farm build = Instantiate(build.prefab) as Farm; as I get an error "Use of unassigned local variable".

I tried adding a prorperty above the if statement "GameObject build", I tried doing Farm build = new Farm() above the if statement also but still get the initial error.

I have a folder titled "Resources" and confirmed a prefab labelled "Farm" is there also.

Resources Folder

Could it have anything to do with the "static" prefix for the method?

  • 1
    \$\begingroup\$ Have you tried doing public Trasform farmPrefab; in the beginning and drag and drop the prefab from the inspector? then on Instantiate instead of Resources.Load("Farm") you can just put farmPrefab. \$\endgroup\$ Apr 13, 2018 at 15:57
  • \$\begingroup\$ Thank you. That does solve the issue, but why didn't my code work? Everything seemed correct and I was calling the Resources.Load() correctly... \$\endgroup\$
    – Anon
    Apr 15, 2018 at 11:25
  • \$\begingroup\$ Game Dev doesn't have a canonical dupe for this, but stack overflow does. \$\endgroup\$ May 16, 2018 at 15:37

2 Answers 2


You can specify a Type when you call Resources Load. Try loading the object in as a gameobject, instantiating it and then calling GetComponent() to retrieve the farm component.

I am guessing that the Farm is a script that you wrote and attached to a GameObject? If so, it is a component so you'd want to load/instantiate the GameObject and get the component

GameObject prefab = Resources.Load<GameObject>("Farm") as GameObject;
GameObject farmObject = Instantiate(prefab);
Farm build = farmObject.GetComponent<Farm>();

You could combine these into a line or two but it may be easier to debug like this.

Although, remember that Resources.Load is an expensive operation. So avoid using it unless necessary

  • 2
    \$\begingroup\$ You shouldn't need to use both the type specifier <GameObject> and as - the generic Resources.Load<GameObject> already returns the type inside the angle brackets, without the need for a cast. Note too that if what you want ultimately is a Farm, it suffices to load & instantiate a Farm type (its attached GameObject will come with it) \$\endgroup\$
    – DMGregory
    May 16, 2018 at 13:55

When using Resources.Load(string) you have to supply full path of a file. I'm not the most familiar with it, but I assume the problem with your code is that you put just "Farm" instead of "Farm.prefab" (file extensions do not show up in Unity's UI). Edit: This was a wrong assumption, based on Unity's Resource.Load documentation, "extensions must be omitted". (Thanks @DMGregory)

Personally I prefer using public Transform farmPrefab; in the beginning of the file, then drag and drop the prefab from the inspector, and use Instantiate(farmPrefab) from the script. It makes things easier when you want to rename the prefab, or use another one, without editing the code itself.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .