First of all, you have to either create a public variable public ScrollingObjectCollection scrollingCollection;
in your script and assign the component in the Unity-Editor or use GameObject.Find("MyGameObjectName").GetComponent<ScrollingObjectCollection>()
to assign the variable in the Start() method.
After instantiating your buttons you can call the scrollingCollection.UpdateContent()
method.
However, if you are using a GridObjectCollection inside your ScrollingObjectCollection in order to layout the children you have to create a variable of that type aswell, assign the GridObjectCollection script and call objectCollection.UpdateCollection()
before calling scrollingCollection.UpdateContent()
.
As this answer states, this should be done inside a coroutine on order to work as expected.
Here's an excerpt from my code:
public GridObjectCollection objectCollection;
public ScrollingObjectCollection scrollingCollection;
private void Start()
{
InsertIntoObjectCollection(20);
}
public void InsertIntoObjectCollection(int count)
{
StartCoroutine(InsertIntoObjectCollection_Coroutine(count));
}
public IEnumerator InsertIntoObjectCollection_Coroutine(int count)
{
for (int i = 0; i < count; i++)
{
var newErrorMessage = Instantiate(singleErrorMessagePrefab, objectCollection.transform);
}
yield return new WaitForEndOfFrame();
objectCollection.UpdateCollection();
yield return new WaitForEndOfFrame();
scrollingCollection.UpdateContent();
}