Instantiated prefab not scaling with screen size

My UI button scales perfectly in the scene but when I make it a prefab to be instantiated later, it does not scale to fit the height of the window. Does it have something to do with the grid layout group script? There are no errors in a problem like this so it is hard to debug

Depends on how you instantiate it. The Instantiate method has a bool (stayinworldspace?) which affects the scale of the instantiated object.
You want your object to have a scale of 1,1,1. Which means the boolean should be set to false (out of mind).

am on my phone atm so can’t check.

1 Like

here is how i instantiate the UI button:

GameObject i;
if(collision.gameObject.tag == “item”)
{
i = Instantiate(inventoryIcons[0]);
i.transform.SetParent(inventoryPanel.transform);

}

Instantiate(Object original, Transform parent, bool instantiateInWorldSpace)

Use this form instead. Get the icon from the array and use that as “original”, inventoryPanel.transform as “parent” and then false as third parameter.

then you don’t need to use setparent.

also .tag == “string value” is inefficient.
There is a CompareTag method which has better performance :wink:

imho… unity should set the tag property to private and only use methods for comparing and setting the tag. It avoids using the inefficient way of comparing a tag.

3 Likes

Thanks so much for helping me but I have no idea where I’m supposed to put that code. I should learn more about C# but to be honest I am just trying to get a working prototype of my game. If you could maybe show me where that could you provided goes that would help me out even more but this is a good place to start none the less… thanks

Hey nevermind I figured it out! I ended up doing this:

i = Instantiate(inventoryIcons[0], inventoryPanel.transform, instantiateInWorldSpace);

and it works perfectly. Thanks again!!!