- Home /
Resource.Load prefab via variable?
I'm slowly going insane (by Vaas' defineeeetion of insanity at least); I am making a game similar to Bit.Trip: Beat as a fun little side project, but this problem I can't seem to fix and the internet isn't helping!
I have a list that acts as a queue for when to spawn different types of entities, e being a string of one of those variables in the list, and I want to load them dynamically so I can avoid a ridiculous amount of if/else statements that I no doubt will have. this is the line of interest:
GameObject p = Resources.Load("Prefabs/" + e.z);
Whenever I try to Resources.Load
with a variable (even if it's a string that I KNOW is right,) it will spit out an error of the prefab being null, but when I use
GameObject p = Resources.Load("Prefabs/Basic Pip");
'Basic Pip' being the name of the current test entity, it will work fine!
Answer by Jeff-Kesselman · Nov 15, 2014 at 12:28 AM
try this code instead
string path = "Prefabs/" + e.z;
Debug.Log("Load path is "+path);
GameObject p = Resources.Load(path);
Chances are e.z isnt what you think it is.
Debugging is the art of identifying and testing every assumption you are making until you discover one that is false.
Doesn't work; In tests, using
Debug.Log("Prefabs/" + e.z);
spits out "Prefabs/Basic Pip". If I directly use
Resource.Load("Prefabs/Basic Pip")
it works fine, but it seems that using any variable in Resource.Load results in it returning null.
...And therein lies my problem
I suspect you have non printing characters in the variable string because I promise you if its the same exact value C# doesn't care about where it came from. There is no way the called code could possibly even know the value came from a variable v. a constant string.
Try this, now..
//string path = "Prefabs/" + e.z;
string path = "Prefabs/Basic Pip";
Debug.Log("Load path is "+path);
GameObject p = Resources.Load(path);
And print the path string you are really passing DONT reassmble it in the Debug.Log.
Doing the latter introduces an unproved assumption that they are assembled the same in both cases, and in debugging you want to avoid introducing new assumptions at all costs.
I would look closely at where the value of e.z is co$$anonymous$$g from. $$anonymous$$aybe you could show us that code? $$anonymous$$aybe there is a newline on the end.
You might want to print:
Debug.Log(e.z+"!");
If the ! point prints on the next line then there is a newline at the end you don't want.
What I'm doing is taking each line in a text file and splitting it at /n
The contents are: 5.0,0.0,Basic Pip 10.0,0.0,Basic Pip
then I split each line at the , into 2 floats and a string, e.z is the string
ie:
[5.0,0.0,Basic Pip\n 10.0,0.0,Basic Pip] the first is dealt with individually [5.0,0.0,Basic Pip] the three are separated into the float x, the float y, and the string z [5.0][0.0][Basic Pip]
Your answer
![](https://koobas.hobune.stream/wayback/20220613171531im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Whats the best way to populate a Dictionary with multiple Prefabs? 0 Answers
Unity not Instantiating Prefabs Properly 0 Answers
how do I assign a prefab's child to another prefab's script's public variable? 1 Answer
Ways to Avoid conflict between prefabs in source control 2 Answers
is having a Prefab reference inside a a Scriptable Object legit? 0 Answers