- Home /
The question is answered, right answer was accepted
Some Prefabs are not Loading
I am using six prefabs right now: Cross, Straight, Curve, End, Tee, Blank. All of them load and are able to be instantiated, except for End and Tee, which when I try to instantiate either, give the error "ArgumentException: The prefab you want to instantiate is null."
I load everything here:
public GameObject fabCross_Tile = Resources.Load("Cross") as GameObject;
public GameObject fabStraight_Tile = Resources.Load("Straight") as GameObject;
public GameObject fabCurve_Tile = Resources.Load("Curve") as GameObject;
public GameObject fabEnd_Tile = Resources.Load("End") as GameObject;
public GameObject fabTee_Tile = Resources.Load("Tee") as GameObject;
public GameObject fabBlank_Tile = Resources.Load("Blank") as GameObject;
I initialize everything here:
public void initialize_cross ()
{
entity = fabCross_Tile;
t_top = 1;
t_right = 1;
t_bottom = 1;
t_left = 1;
print (entity);
}
public void initialize_straight ()
{
entity = fabStraight_Tile;
t_top = 1;
t_right = -1;
t_bottom = 1;
t_left = -1;
print (entity);
}
public void initialize_curve ()
{
entity = fabCurve_Tile;
t_top = 1;
t_right = -1;
t_bottom = -1;
t_left = 1;
print (entity);
}
public void initialize_end ()
{
entity = fabEnd_Tile;
t_top = 1;
t_right = -1;
t_bottom = -1;
t_left = -1;
print (entity);
}
public void initialize_tee ()
{
entity = fabTee_Tile;
t_top = 1;
t_right = 1;
t_bottom = -1;
t_left = 1;
print (entity);
}
public void initialize_blank ()
{
entity = fabBlank_Tile;
t_top = -1;
t_right = -1;
t_bottom = -1;
t_left = -1;
print (entity);
}
At this point, it prints out Null for End and Tee, but the correct values for everything else.
If it's returning null, then it can't find it.
You should check the spelling for the "End" and "Tee" within your resources to make sure you're loading the exact file.
Answer by eskivor · Jun 15, 2017 at 07:59 AM
are these prefabs well named in the resources folder ?
Hmm ... I tried changing the names to a few different things, and anything other than "End" and "Tee" seemed to work, though I have no idea why.
Are you sure that
public GameObject fabEnd_Tile = Resources.Load("End") as GameObject;
public GameObject fabTee_Tile = Resources.Load("Tee") as GameObject;
is executed ? Your script is not entire here, do you execute all these lines in only one function or separate ones ?
You do not uploaded the scripts that also execute the initialize
functions, can you do it, are you sure that all of these initialize
functions are executed ?
Ultimately, if it's still not those solutions just above, can you upload your project somewhere online and share a link in this topic ? (If it's a big project, create a empty new one, and put it only the problematic parts)
Otherwise, your initialize
script can be refactored to something way more shorter like this (while assu$$anonymous$$g that t_top
, t_right
, t_bottom
, t_left
are variables of type int
, else replace int
by their correct type (float, etc.)) :
public void Initialize (GameObject prefabObject, int t_topValue, int t_rightValue, int t_bottomValue, int t_leftValue)
{
entity = prefabObject;
t_top = t_topValue;
t_right = t_rightValue;
t_bottom = t_bottomValue;
t_left = t_leftValue;
print (entity);
}
public void initialize_cross ()
{
Initialize (fabCross_Tile, 1, 1, 1, 1);
}
public void initialize_straight ()
{
Initialize (fabStraight_Tile, 1, -1, 1, 1);
}
public void initialize_curve ()
{
Initialize (fabCurve_Tile, 1, -1, -1, 1);
}
public void initialize_end ()
{
Initialize (fabEnd_Tile, 1, -1, -1, -1);
}
public void initialize_tee ()
{
Initialize (fabTee_Tile, 1, 1, -1, 1);
}
public void initialize_blank ()
{
Initialize (fabBlank_Tile, -1, -1, -1, -1);
}