Author Topic: Problem when creating GUI elements at runtime  (Read 1490 times)

xbelt

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 9
    • View Profile
Problem when creating GUI elements at runtime
« on: June 09, 2014, 03:23:05 AM »
Hello,

I am currently trying to spawn some buttons at runtime. For testing purposes the Button transform is just the default NGUI button prefab.

I am using the following code:

  1. var go = NGUITools.AddChild(Parent.gameObject, Button);

Also the parent is just another NGUI element.

Now when executing this code it just creates an empty gameObject. I verified that the Button Prefab is in fact not empty.
Thanks for your help and time

Lukas

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem when creating GUI elements at runtime
« Reply #1 on: June 09, 2014, 03:30:54 AM »
Look at the code for that function:
  1.         static public GameObject AddChild (GameObject parent, GameObject prefab)
  2.         {
  3.                 GameObject go = GameObject.Instantiate(prefab) as GameObject;
  4. #if UNITY_EDITOR
  5.                 UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Create Object");
  6. #endif
  7.                 if (go != null && parent != null)
  8.                 {
  9.                         Transform t = go.transform;
  10.                         t.parent = parent.transform;
  11.                         t.localPosition = Vector3.zero;
  12.                         t.localRotation = Quaternion.identity;
  13.                         t.localScale = Vector3.one;
  14.                         go.layer = parent.layer;
  15.                 }
  16.                 return go;
  17.         }
Try putting Debug.Log in there to see what it's actually doing. You can find it in NGUITools.cs, line 467.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Problem when creating GUI elements at runtime
« Reply #2 on: June 09, 2014, 12:04:01 PM »
Are you sure that the "Button" actually references the prefab correctly?