Skip to main content
added 46 characters in body
Source Link
Hamza Hasan
  • 3.4k
  • 2
  • 16
  • 24

Instantiate will always give you an object. The better way to do that is first convert it in GameObject then access your script from there.

GameObject go = Instantiate (myButtonPrefab) as GameObject;
btnController bc = go.GetComponent<btnController>();
bc.Setup(name,0);

It is for when your prefab has already btnController attached.

If you want to add script, do that as,

btnController bc = go.AddComponent<btnController>();
bc.Setup(name,0);

Instantiate will always give you an object. The better way to do that is first convert it in GameObject then access your script from there.

GameObject go = Instantiate (myButtonPrefab) as GameObject;
btnController bc = go.GetComponent<btnController>();

It is for when your prefab has already btnController attached.

If you want to add script, do that as,

btnController bc = go.AddComponent<btnController>();

Instantiate will always give you an object. The better way to do that is first convert it in GameObject then access your script from there.

GameObject go = Instantiate (myButtonPrefab) as GameObject;
btnController bc = go.GetComponent<btnController>();
bc.Setup(name,0);

It is for when your prefab has already btnController attached.

If you want to add script, do that as,

btnController bc = go.AddComponent<btnController>();
bc.Setup(name,0);
Source Link
Hamza Hasan
  • 3.4k
  • 2
  • 16
  • 24

Instantiate will always give you an object. The better way to do that is first convert it in GameObject then access your script from there.

GameObject go = Instantiate (myButtonPrefab) as GameObject;
btnController bc = go.GetComponent<btnController>();

It is for when your prefab has already btnController attached.

If you want to add script, do that as,

btnController bc = go.AddComponent<btnController>();