The Object you want to instantiate is null.
Hello,
I am trying to instantiate a prefab from resources folder using the name from a text file located in my resources folder. Prefab is also located in my resources folder. However, I get an error saying "the object I want to instantiate is null".
When I directly give the name of prefab it works. I cannot seem to understand the issue.
My code is :
public void Loadd() {
TextAsset txtasset = (TextAsset)Resources.Load (txtfile);
txtcontents = txtasset.text;
string[] linesinfile = txtasset.text.Split ('\n');
for(int i= 0; i<1; i++){
for (int x = 0; x < buttonholder.Length; x++) {
//GameObject Instance = Instantiate (Resources.Load ("F260001", typeof(GameObject))) as GameObject; // works
string name = linesinfile [x];
Debug.Log (name);
GameObject Instance = Instantiate (Resources.Load (name, typeof(GameObject))) as GameObject;
}
}
}
if the log right before the error looks correct, there are spaces before or after the string
The log does come right, I have checked the text file, there are no spaces before and after.
I am attaching a image of my text file. Each object name is in a new line.
Some text apps add a new line and a carriage return character. You might want to do an explicit check to see that linesinfile [0] == "F260001".
It seems you are correct. I added a check if (linesinfile [0] == "F260001") before instantiation, now there are no errors since condition comes to false. However, I am confused with the Debug.log because both Debug.log(name) and Debug.log(linesinfile[o]) shows the same thing in console.
I am attaching screenshots. **
I also tried to check if the length of both strings name and linesinfile[0] are different. However, they show to be of the same length. I am attaching screenshots of code and Debug.log .
I think special characters (like \n, \r, \t) do not count towards length.
Answer by corpsinheretoo · Feb 10, 2017 at 05:48 AM
If linesinfile [0] == "F260001" is false and yet they look the same in the console then there is something different about the strings that is not showing up in console. As @hexagonius suggested that might be a space; however you checked for that. You split the file string on \n but some text editors add \r\n when you press return. So the string you thought looked like this: F26001\nF26002\nF26003 Really looks like this: F26001\r\nF26002\r\nF26003
Try using a different text editor (and checking the invisible characters) or set up your textfile with a different separator; e.g.: F26001;F26002;F26003 Then use: txtasset.text.Split (';');
You were right, it was an issue with the text editor. I used your approach and ins$$anonymous$$d of splitting with \n, used ';' and it worked. Thanks a lot.
Your answer
Follow this Question
Related Questions
checking if gameobject exists 1 Answer
How to Serialize or Save a List of GameObjects 1 Answer
Unnamed gameobjects appear in Unity4 when opening Unity5 project 0 Answers
Set Click area in Game 1 Answer
Accessing values on a gameobject 1 Answer