0

I have this initialization of a prefab:

GameObject e = Instantiate(entry, new Vector3(x, y, 0), Quaternion.identity) as GameObject;

I know that it is successfully instantiated because I can see a clone on my hierarchy. However, if I try to access it, there is a NullReferenceException. I tried this:

Debug.Log(e.ToString());

And this:

e.transform.SetParent (GameObject.Find("Canvas").transform);

But it shows this error

NullReferenceException: Object reference not set to an instance of an object StudentListController.FillList (System.String strList) (at Assets/Scripts/StudentListController.cs:45) StudentListController+c__Iterator5.MoveNext () (at Assets/Scripts/StudentListController.cs:23)

This is the code of FillList:

void FillList(string strList) {
    string[] studentData;
    string[] studentList = strList.Split (';');
    int startX = -253;
    int startY = 70;
    int addX = 100;
    int addY = 30;
    for (int y = 0; y<studentList.Length; y++ ) {
        string student = studentList[y];
        studentData = student.Split(',');
        for(int x = 0; x<studentData.Length; x++) {
            string data = studentData[x];
            if (x==3) {
                data = getAgeFromBirthdate(data).ToString();
            } else if (x==4) {
                data = getSex(data).ToString();
            } 
            GameObject e = Instantiate(entry, new Vector3(startX + 100*x, startY + 40*y, 0), Quaternion.identity) as GameObject;
            //e.GetComponent<Text> ().text = data;
            //e.transform.SetParent (GameObject.Find("Canvas").transform);
        }
    }

}
mwilczynski
  • 3,062
  • 1
  • 17
  • 27
jeanl
  • 379
  • 2
  • 7
  • 18
  • your e variable is not throwing the exception , seems like the exception is being thrown by strList in the FillList method of your StudentListController – mahdi mahzouni Jan 17 '16 at 08:46
  • so what should i do? – jeanl Jan 17 '16 at 08:57
  • paste the Fill method code here too , and see if newing up the strList helps , my guess is you don't new up the strList or your code is not providing it with value so it is null hence the exception – mahdi mahzouni Jan 17 '16 at 09:51
  • done adding the code for FillList – jeanl Jan 17 '16 at 09:53
  • sorry to ask but do strList , studentList and studentData have value ? also try casting the Instantiate return type and don't use the as because it will fail silently , if it fails to cast it , try it like this : (GameObject)Instantiate(.....) – mahdi mahzouni Jan 17 '16 at 10:15

1 Answers1

0

Check if you have dragged and dropped anything you need in your custom components for the prefab. If you drag and drop them you should then click apply.