- Home /
All child objects has a localPosition.y of 0
I have a parent object which contains a Grid Layout Group. After a button is clicked, I instantiate couple Raw Images to the parent GameObject, but when I Debug.Log the localPosition.y of those Raw Images - they all are 0, even though they aren't in the same place, but they are anchored to the same point and have the same parent. And the Inspector shows the actual position, not 0.
If you call Debug.Log
right after instantiating the objects, the layout component did not have the time to place the elements. You will need to wait the next frame before getting the correct values.
You can use a coroutine to do this:
void InstantiateImages()
{
for( int i = 0 ; i < ... ; ++i )
{
// Create images, parent it to layout
}
StartCoroutine( WaitLayoutComplete() ) ;
}
IEnumerator WaitLayoutComplete()
{
yield return null ;
// Loop through the instantiated objects
for( int i = 0 ; i < ... ; ++i )
{
// Log position
}
}
Your answer
Follow this Question
Related Questions
Positioning a GameObject relative to its bottom 1 Answer
Creating a Procedural Grid positioning problems 0 Answers
How to anchor a (Image, Button, Slider) to Grid create by a script? 0 Answers
How can I position a number of gameObjects on one line and then move them together? 1 Answer
How can i change cell grid position dynamically in unity 0 Answers