- Home /
Question by
Goat_Master · Oct 22, 2014 at 05:40 PM ·
uibuttonunity 4.6
[Unity 4.6] UI Button problem null reference exception [Js]
So i got a few problems with this code. The function gets called when i press the "Refresh Hosts" button. If it finds a host the function gets called. Wich works fine.
But I got some problems accessing the buttons that gets created in runtime. I get a NullReferenceException when I try to define some variables in the components of the instantiated button.
Help would be greatly appreciated. :D
function UIHostDataButtons(){
if(hostData){
for(var i:int=0; i<hostData.Length; i++){
var button = new GameObject("button", CanvasRenderer, Image, Button);
var buttonScript = button.GetComponent(Button);
Debug.Log("Chupset");
button.transform.SetParent(canvas.transform, false);
button.id = "Game" + i;
button.RectTransform.sizeDelta = Vector2(200, 30);
button.GetComponentInChildren(Text).text = hostData[i].gameName;
button.RectTransform.posX= 0;
button.RectTransform.posY= 50 / i;
button.buttonScript.onClick.AddListener( function() {connectToHost(i);} );
}
}
}
Comment