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)
return;
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.
Could it have anything to do with the "static" prefix for the method?
public Trasform farmPrefab;
in the beginning and drag and drop the prefab from the inspector? then onInstantiate
instead ofResources.Load("Farm")
you can just putfarmPrefab
. \$\endgroup\$