Question by
stefan52b · Feb 21, 2016 at 11:03 PM ·
buttongetcomponentnull exception
versions 5.3.1.p3: newButton.GetComponent() returns null
Does anybody know why tempButton always returns null:
public void GoDirectoryDeeper(string dir) { OpenDir(dir); } void Start() { GameObject newItem = Instantiate(itemPrefab) as GameObject; GameObject newButton = Instantiate(prefabButton) as GameObject; newButton.transform.SetParent(newItem.transform, false); newButton.transform.localScale = new Vector3(1, 1, 1); Button tempButton = newButton.GetComponent<Button>(); string a = "dir1"; tempButton.onClick.AddListener(() => GoDirectoryDeeper(a)); }
Comment
Best Answer
Answer by jgodfrey · Feb 21, 2016 at 11:36 PM
Because, apparently, the "newButton" object doesn't contain a component of type "Button".
Thanks! I had attached, in the inspector, a self defined button to the prefabButton, after I reassigned a standard Button the null exception went away.