Instantiating a prefab and adding it to a RectTransform sets abnormal values to its local scale - unity3d

If you instantiate a prefab and parent it to an object under a UI Canvas or any RectTransform really, its scale would have "abnormal" values, in my case, they were equal to the reference Pixels Per Unit value. Why? Is this a bug or is this behavior by design?

I was having issue parenting UI items, then I started using:
rectTransform.SetParent(parentTransform, false);
and it solved it. Notice the second parameter, it indicates you do not wish the transform values to be in world space but in local space of the parent instead.
That could be it for you too.

Related

My prefabs' size appears wrong when placed

So on a game I am working on, I keep having the same problem where whenever I turn an object into a prefab, somehow the object always appears smaller but the width and height says otherwise. I can't seem to fix this problem and it also applies whenever I try to instantiate it, it always remains that way even when I just create a prefab.
The Prefab as an example
The image I screenshot shows what happens whenever I assign or make an object as a prefab. Am I doing something stupid? or is it just because there is a setting I am not aware of?
In unity ths scale of an object affected by it's parent scale..
so for example if I have a cube with width 10 unit and it is scale is [10,1,1]
and I convert it to prefab every time Iniciate object from it will be in the same scale..
the problem apper if my cube was a chiled for other game object with scale for example [5,5,5]...
as you see in this picture
this is the same prefab but the size is totaly diffrent because of the parent
to avoid this proble be sure always to make your prefab with scale 1 1 1
you can create an empty game object with scale 1 1 1 and position[0,0,0] then make your object child of it then make your prefab ...
:) and if you want further help pleace give more specific informations for your problem

Unity GameObject Width and Height set to zero but still displaying as normal

I have a GameObject in a scene but not in the Canvas. In this GameObject I am instantiating a couple of other GameObjects without any problems.
What I do not understand is that, it doesn't matter if I set the main GameObject's Width and Height to 0 or 100 or 1000, nothing changes, it will still instantiate and display everything in the same way.
Can someone please explain me why it still displaying everything when I set the main GameObject's width and height to 0?
Thank you.
RectTransforms only matter to Canvas objects
Width and Height are properties only available to RectTransform type Transforms and only matter for Canvas objects. For non-canvas objects you should just use a normal Transform at which point you will have a scale parameter, which setting it to zero, will make the object have zero size.

Unity - Child Rotation and Scale error

I'm saw some wierd responses from Unity when I tried to do these actions :
1- I've created a Plane, simply rotated it (-32,-19,-56) and scaled it (3.21,1,2.43)
2- I've created a child cube for that plane and rotated it (-30,39,-78) and scaled it (3.54,4.8,6.42)
This is the final result in editor :
Did I do something wrong???
Why collider and renderer performing differently??
Is this a Unity bug? I'm currently using Unity 2017.3.1p1
Yes, it is some kind of bug.
To avoid it, don't child the objects directly to a non-uniformly scaled parent. Instead, child this non-uniform object (the cube) to an empty object, which will be the parent of the plane too.
Since an empty object is scaled 1,1,1, childing other objects to it don't cause this problem.

Unity 4.6: Button instances added at runtime are not scaled by Reference Resolution?

(Using Unity 4.6.0b20)
I've hit a problem where prefab button size works correctly when added in editor but seems to ignore Reference Resolution when added by script.
This uses a Canvas with Reference Resolution 1280x720 and MatchWidthOrHeight. Canvas has a Panel Vertical Layout Group for the buttons. The Button has Preferred Width/Height, and it is saved as a Prefab so new instances can be created from assets at runtime.
In the editor I can drag the prefab to scene to add instances to the panel which also has width 102 and they stack and scale nicely:
But when instead I add new instances of the prefab via script to the Panel they appear with the wrong size. Looking at the dimensions my guess is the 102 pixel size is not being scaled by the Reference Resolution:
The script code creates the instance via GameObject.Instantiate() and adds to the Panel by setting transform.parent:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;
I assume either there is more that needs to be done when adding the Button to the Panel than just setting Parent, or it's a bug with the beta...
Suggestions?
Use the method transform.SetParent passing false in the second parameter:
transform.SetParent(transform, false);
This way you can prevent world positioning and scaling.
You can find more information in the unity manual, searching for "UICreateFromScripting".
I found the cause -- when the prefab is added by script the Scale values are set incorrectly; in this test Scale was being set to 1.186284.
But if the script sets scale to 1.0 immediately after adding the button:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;
// HACK force scale to 1.0
uiInstance.transform.localScale = Vector3.one;
the new button instances are sized correctly.
imho this is a Unity bug because adding the prefab instance in the editor sets scale 1.0, the prefab itself has scale 1.0, so I can't see a reason why adding it from script should act differently. But I'm kind of guessing as there's not much documentation for the new UI yet :)
This is not a bug, its an order of execution issue. When using Unity's new layout components the values are driven by the layout components, which get calculated once at the end of the frame that a layout rebuild call is made. If you're basing anything in a script off of the new rectTransform.rect calculations which are driven by the layout elements, you'll need to wait until this calculation has been performed or call it yourself by using
LayoutRebuilder.MarkLayoutForRebuild (transform as RectTransform);
You'll still have to wait until the end of the frame for those calculations to take place before using anything from the rectTransform like scale, pos, etc. This had me stumped for a while when I first start dynamically creating UI elements and the scale was zero depending upon when I instantiated them.
You can read about that rebuild call and the order of execution of the layout calc stuff here:
http://docs.unity3d.com/Manual/UIAutoLayout.html

Unity3d Transform issues when trying to re-parent a Gameobject with sub-Gameobjects

I am trying to grab a Gameobject that's placed in the correct spot and re-parent it to different Gameobject transforms using code. I manage to do this by using ..transform.parent = parent.transform but the rotation and position get messed up. How can I keep it's rotation and position when re-parenting?
Thanks!
Always use gameObject.transform.SetParent(anotherGameObject), or you risk exactly the sort of phenomena you've described. See SetParent docs; there is a second parameter to consider.
The form mentioned in the question still works in many cases, but has been deprecated for a while.
Place all game object that you want to parent, to empty game object(example:"EMPTY\PARENT\EMPTY\CHILD") with scale 1:1:1(in editor)or re-scale your parent game object to 1:1:1