Author Topic: adding child to grid and modifying prefab?  (Read 3033 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
adding child to grid and modifying prefab?
« on: August 30, 2012, 01:15:37 PM »
i have managed to get scrollview, grid and adding children to it to work fine. i have one problem though and that is modifing the child after it was added to the grid. here is the code i have currently (script attached to the button for adding children):


  1. public GameObject Grid;
  2.         public GameObject Prefab; //button with backgroun and label
  3.  
  4.         void OnClick()
  5.         {
  6.                 Prefab.gameObject.name = Random.Range(0,100).ToString(); // just test to see if i can change the name before it is added to the grid - works!          
  7.                 NGUITools.AddChild(Grid,Prefab).GetComponent<UILabel>().text = Random.Range(0,100).ToString(); //this does not work? i need to modify the label text so that i can get something meaningfull
  8.                 Grid.GetComponent<UIGrid>().repositionNow = true;
  9.         }

thanks!

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: adding child to grid and modifying prefab? (SOLVED)
« Reply #1 on: August 30, 2012, 01:47:47 PM »
ok i managed to do it properly like this:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class HierarchyTest : MonoBehaviour {
  5.        
  6.         public GameObject Grid;
  7.         public GameObject Prefab;
  8.         public GameObject DragablePanel;
  9.        
  10.        
  11.         void OnClick()
  12.         {
  13.                 GameObject _go = NGUITools.AddChild(Grid,Prefab);
  14.                 _go.GetComponentInChildren<UILabel>().text = Random.Range(0,100).ToString();
  15.                 _go.name = Random.Range(0,100).ToString();
  16.                
  17.                 Grid.GetComponent<UIGrid>().repositionNow = true;
  18.                 DragablePanel.GetComponent<UIDraggablePanel>().ResetPosition();
  19.         }
  20. }