0
\$\begingroup\$

I have a small container, where I want listed a series of buttons. I try to fill them at Runtime like this:

using UnityEngine;

public class FillWithGearData : MonoBehaviour {
    public Transform SelectGearButton;
    public Transform GearPanel;

    void Awake() {
        Transform button = Instantiate(SelectGearButton, new Vector3(0, 0, 0), Quaternion.identity) as Transform;
        button.SetParent(GearPanel);

    }
}

But the buttons added at Runtime are not adapting to the Layout, even though the ones added before runtime seem to adapt without a problem.

Demo:

enter image description here

What is going on?

\$\endgroup\$
1
  • \$\begingroup\$ Have you tried passing false to SetParent's second parameter ? \$\endgroup\$
    – James0124
    Commented Feb 2, 2016 at 15:42

1 Answer 1

1
\$\begingroup\$

Your clone button's scale is Zero. Set its scale to one after instantiating it.

public GameObject SelectGearButton;
public GameObject GearPanel;

GameObject button = Instantiate(SelectGearButton, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
button.transform.SetParent(GearPanel.transform);
button.transform.localScale = Vector3.one;
\$\endgroup\$
4
  • \$\begingroup\$ That did not work :( \$\endgroup\$ Commented Feb 2, 2016 at 14:32
  • \$\begingroup\$ Yea, that answer one minor mistake, well, I'd suggest to use GameObject \$\endgroup\$ Commented Feb 2, 2016 at 14:38
  • \$\begingroup\$ Check the edits \$\endgroup\$ Commented Feb 2, 2016 at 14:38
  • \$\begingroup\$ The issue was that changing the localScale had to be done AFTER setting the parent. Maybe a bug in Untiy? I am unsure, but thanks for the answer in any case! :) \$\endgroup\$ Commented Feb 2, 2016 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .