Author Topic: Initialising scrollview grid starting items  (Read 399 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Initialising scrollview grid starting items
« on: March 12, 2017, 03:43:06 PM »
I'm populating a grid procedurally and am having trouble setting the start item. Let's say there are six items and I want to start on Item 4, my equipped item, in the centre and selected. I have an OnCenter callback on a UICenterOnChild component to register changes to the selection when the player selects a new item. The problem is this is called when the script starts, changing my equipped item to Item 1.

This is on a custom script on the UIGrid object after the grid has been populated.
  1.         public void Init(UI_designControl controller, int startIndx){
  2.                 buildControl = controller;
  3.                 currentIndex = startIndx;
  4.                 currentItem = transform.GetChild (startIndx).name;
  5.                 GetComponent<UICenterOnChild>().CenterOn (transform.GetChild (startIndx));
  6.                 GetComponent<UICenterOnChild>().onCenter = ChangeDesign;
  7.         }
  9.         void ChangeDesign(GameObject go){
  10.                 if( != currentItem){
  11.                         print("change Design "+currentItem+"    ";
  12.                         currentItem =;
  13.                         buildControl.DesignChanged(gameObject);
  14.                 }
  15.         }

I also am unable to change the grid's position without it animating. Is there a way to call CenterOn() skipping the animation and jumping straight there?


  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,149
  • Toronto, Canada
    • View Profile
Re: Initialising scrollview grid starting items
« Reply #1 on: March 15, 2017, 01:22:05 PM »
Yeah... CenterOnChild was written as a simple script that assumes that it's entirely responsible for centering on objects, and user trying to center on something first is not a part of its thinking. That said, why not just take your script, change its script execution order to run after UICenterOnChild, and do your centering code in its Start()? You will then effectively overwrite what UICenterOnChild is doing.