I am trying to create button in C# code using an existing Prefab. All I found on the website was something like
public GameObject a;
public RectTransform ParentPanel;
void Start()
{
for (int i = 0; i < 5; i++)
{
GameObject goButton = (GameObject)Instantiate(a); /error
goButton.transform.SetParent(ParentPanel, false);
goButton.transform.localScale = new Vector3(1, 1, 1);
Button tempButton = goButton.GetComponent<Button>();
}
}
In Unity project I have Prefab called "ButtonPrefab" where I put Button Object. There is an error in below line
UnassignedReferenceException: The variable a of NewBehaviourScript has not been assigned. You probably need to assign the a variable of the NewBehaviourScript script in the inspector.
I am new to Unity. How is it come that not assigned var a can give me button using Instantiate()? And why do I have this error?