Author Topic: scroll view label child via script position problem  (Read 5047 times)

nitzamc

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
scroll view label child via script position problem
« on: August 29, 2014, 07:01:41 PM »
hey guys im creating multiplayer game and i am implementing ngui for menu where appears labels in a scrollview that show available server rooms.and i am instantiating a UILabel prefab it apears on UI Root & then i make it a child of scroll view and it's changing position to random numbers or i dont know what's wrong here is the script ... it may be hard coded,any help would be apreciated.btw im new
  1. void drawLobby()
  2.     {
  3.        
  4.            
  5.             foreach ( RoomInfo room in PhotonNetwork.GetRoomList() )
  6.            {
  7.               GameObject TempLabel = (GameObject)Instantiate(roomListlabel);
  8.              
  9.  
  10.               TempLabel.transform.parent = parentObject;
  11.               TempLabel.GetComponent<UILabel>().text = room.name + "  " + room.playerCount + " / " + room.maxPlayers;
  12.  
  13.              
  14.               TempLabel.transform.localScale = new Vector3(1, 1, 1);
  15.               //TempLabel.transform.position = new Vector3(-309, y, 0);
  16.          
  17.                 y -= 40;
  18.  
  19.           }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scroll view label child via script position problem
« Reply #1 on: August 30, 2014, 09:27:50 AM »
Never use Instantiate() with UI elements. Use NGUITools.AddChild. It sets the parent for you, resets the position/scale, and even sets the layer properly.

nitzamc

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scroll view label child via script position problem
« Reply #2 on: August 30, 2014, 02:48:37 PM »
it automaticaly sets the parent? or the parent is the gameobject that script is attached?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scroll view label child via script position problem
« Reply #3 on: August 31, 2014, 02:53:01 PM »
NGUITools.AddChild(parent, prefab);

You could have just looked at the function and answered your own question. :|

nitzamc

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scroll view label child via script position problem
« Reply #4 on: September 10, 2014, 03:25:40 PM »
you are right... sorry  :-X

i have another problem i uploaded images so i can give u understand because i'm not good english speaker...
so i need it to appear like this in the scroll view after instantiation (addchild)

if i drag the label prefab and drop it as a child in hierarchy it automaticaly apears like this,and that is excactly my goal.but via script it appears like this
« Last Edit: September 10, 2014, 03:55:55 PM by nitzamc »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scroll view label child via script position problem
« Reply #5 on: September 11, 2014, 01:45:24 AM »
Look at the transform position. It's (-116, 208, 0) in the first picture, and (0, 0, 0) in the second. When you do AddChild(), it resets the position. You need something that positions the content properly for you -- like a UIGrid for example.

nitzamc

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scroll view label child via script position problem
« Reply #6 on: September 11, 2014, 08:10:35 PM »
@ArenMook although i've added UIGrid  & nothing changed i  solved it via script , would be better if scripting doesn't involved