Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI Button add to panel via script

Discussion in 'UGUI & TextMesh Pro' started by skyhawk92, Dec 26, 2014.

  1. skyhawk92

    skyhawk92

    Joined:
    Oct 12, 2013
    Posts:
    7
    Hi,

    I'm trying to put button(stored as prefab) dynamically to panel.

    I tried many ways, though I don't understand exactly how it should be done.
    Code (CSharp):
    1. public RectTransform panel;
    2. public Button bt;
    3. .
    4. .
    5. .
    6. Instantiate(bt);
    7. bt.transform.SetParent(panel.transform);
    8.  
    I always get this error:
    "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

    My question is, how to do it correctly ?
     
    Last edited: Dec 26, 2014
  2. skyhawk92

    skyhawk92

    Joined:
    Oct 12, 2013
    Posts:
    7
  3. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    A solution for the rest of us would be appreciated.