Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

prefab instantiated does not fit the panel

Discussion in 'Prefabs' started by imposter_syndrom_incarnated, Sep 23, 2021.

  1. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Hi there! I was working on my leaderboard and the bar with the data of the user is supposed to be instantiated on the screen inside the scrolling panel.
    upload_2021-9-23_1-59-5.png
    I problem is that it is too big and I was not sure how to make sure that it stays the same size as it was saved in the first place.
    This was my code
    Code (CSharp):
    1.  
    2. var onScreen = Instantiate(LBprefab, new Vector3(0, 0, 0), Quaternion.identity);
    3. onScreen.transform.SetParent(panel.transform);
    4.  
    This is how the code was in a tutorial I was following. However the code that worked for me was:
    Code (CSharp):
    1. var onScreen = Instantiate(LBprefab);
    2. onScreen.transform.SetParent(panel.transform, false);
    Can anybody explain why please?
     
    Last edited: Sep 23, 2021
  2. NocturnalWisp

    NocturnalWisp

    Joined:
    Oct 2, 2019
    Posts:
    61
    Hello!
    Based on what you said above, you have fixed the problem? If so, the reason it works with code that you've done as apposed to the code above is because of this line:

    Code (CSharp):
    1. onScreen.transform.SetParent(panel.transform, false);
    The second parameter in the SetParent function provides the "worldPositionStays" boolean which determines whether the instantiated object keeps it's world position or alters it's position to fit in the new parent. You passed false, which causes the object to alter to the new parent resulting in "hopefully" the correct behavior.

    If your issue is not fixed, providing the tutorial could help to gain some insight into the issue, however; one step is to alter the anchor points and scaling of the prefab in the prefab editor. You can change how it will scale or appear when instantiated.
     
  3. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    thank you ! I will try to work with anchor points alterations as well :)
     
  4. NocturnalWisp

    NocturnalWisp

    Joined:
    Oct 2, 2019
    Posts:
    61
    No problems! Hope it all works out! UI can be finicky.
     
unityunity