Reference resolution not working on instantiated prefabs

Hi, I’m trying to instantiate a button and then set it as a child of the canvas. I have referenceResolution in my canvas in order to keep things properly scaled across different resolutions, and this works fine on gameobjects which are already in the canvas.
However, the instantiated button appears to not scale according to this resolution; instead, it looks very small on large resolutions, and very big on small resolutions. I suppose that setting it as the child of the canvas isn’t enough, and there’s something I’m missing here.

Here’s how I instantiate the button, levelMenu is the canvas that has referenceResolution:

GameObject currentButton = Instantiate(levelButton) as GameObject;
            currentButton.transform.position = buttonPositions[showingLevelAmount].transform.position;
            currentButton.transform.parent = levelMenu;

I had a similar problem. Try setting transform.localScale of the new button to Vector3.one after parenting it - this fixed it for me.

1 Like

Using transform.SetParent(targetParent, false) may help - I had all sorts of instantiate issues, I think there’s some internal data in the prefab trying to remember what it looked like the last time it was in the world, then tries to replicate that when instantiated. Using SetParent with false for the second parameter seems to tell it to not do that.

Note: I don’t think this default behavior is ever desirable, especially since one of the perks of this new GUI system is using anchors to get things to auto-conform to spaces

3 Likes

I believe what is happening is that the prefab is that if you do this:

GameObject sliderObj = Instantiate(Resources.Load<GameObject>("PropertyEditor/slider")) as GameObject;
sliderObj.transform.localScale = Vector3.one;

The prefab will either load some saved value, or more likely stomp on them with some data that gets computed as it’s created.

However, if you put a component on the object which is going to be instantiated which does:

void Start()
{
   transform.localScale = Vector3.one;
}

Then it will work fine. This is pretty annoying, because it means you have to make a prefab for everything you ever want to instantiate and add a component to it to do this. It would be nice if instantiate had a scale parameter to pass to it, or if this behavior was corrected.

1 Like

SetParent with “false” is the correct answer to this problem in my experience.

2 Likes

I actually do something like this:

public static class GameObjectExtension
{
    public static GameObject InstantiateUIElement ( this GameObject Parent , GameObject Element )
    {
        GameObject Child = ( GameObject ) GameObject . Instantiate ( Element ) ;

        Child . transform . SetParent ( Parent . transform , false ) ;

        return ( Child ) ;
    }
}

So you can just say levelMenu . InstantiateUIElement ( levelButton ) and go from there.

Thanks for the replies, it helped a lot.

Thanks a lot!! this really helped me!