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

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.

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 + 2D) Change UI Button position issue

The last few days I've been stuck with a headache of a problem in Unity.
Okay, I won't go into details with my game, but I've made a super-simple example which represents my issue.
I have a 2D scene these components:
When the scene loads, and I tap the button this script executes:
Vector3 pos = transform.position;
pos.x -= 10;
transform.position = pos;
I have also tried this code:
transform.position = Camera.main.WorldToScreenPoint(new Vector3(0, 0, 0));
The problem is, that when I click the button, the x-pos of the object sets to -1536 which is not as expected. Picture shows the scene after the button has been clicked. Notice the Rect Transform values:
So I did a little Googling and found out about ScreenToWorldPoint, WorldToScreenPoint etc but no of these conversions solves my problem.
I'm pretty sure I'm missing someting here, which probably is right in front of my, but I simply can't figure out what.
I hope someone can point me in the right direction.
Best regards.
The issue is that you are using transform.position instead of rectTransform.anchoredPosition.
While it's true that UI elements are still GameObjects and do have the normal Transform component accessible in script, what you see in the editor is a representation of the RectTransform that is built on top. This is a special inspector window for UI elements, which use the anchored positioning system so you can specify how the edges line up with their parent elements.
When you set a GameObject's transform.position, you are providing a world space position specified in 3D scene units (meters by default). This is different from a local position relative to the canvas or parent UI element, specified in reference pixels (the reference pixel size is determined by the canvas "Reference Resolution" field).
A potential issue with your use of Camera.WorldToScreenPoint is that that function returns a position specified in pixels. Whereas, as mentioned before, setting the transform.position is specified in scene units (i.e. meters by default) and not relative to the parent UI element. The inspector, though, knows it's a UI element so instead of showing you that value, it is showing you the world position translated to the UI's local coordinates.
In other words, when you set the position to zero, you are getting the indices of whatever pixels happen to be over the scene's zero point in your main camera's view, and converting those pixel numbers to meters, and moving the element there. The editor is showing you a position in reference pixels (which are not necessarily screen pixels, check your canvas setting) relative to the object's parent UI element. To verify, try tilting your camera a bit and note that the value displayed will be different.
So again you would need to use rectTransform.anchoredPosition, and you would further need to ensure that the canvas resolution is the same as your screen resolution (or do the math to account for the difference). The way the object is anchored will also matter for what the rectTransform values refer to.
Try using transform.localposition = new Vector3(0,0,0); as your button is a child of multiple game objects. You could also try using transform.TransformPoint, which should just convert localpos to worldpos.
The issue is that your button is inside of another object. You want to be changing the local position. transform.localPosition -= new Vector3(10, 0, 0)
As #Joseph has clearly explained, you have to make changes on your RectTransform for your UI components, instead of change Transform.
To achieve what you want, do it like this:
RectTransform rectTransform = this.GetComponent<RectTransform>();
Vector2 anchoredPos = rectTransform.anchoredPosition;
anchoredPos.x -= 10;
rectTransform.anchoredPosition = anchoredPos;
Just keep in mind that this 10 are not your 3D world space units (like meters or centimeters).
Try these things because I did not understand what you were trying to do
Try using transform.deltaposition
Go to the canvas and go then scale with screen size then! You can use transform.position = new Vector3(transform.position.x -10,transform.position.y, transform.positon.z)
And if this doesn't work
transform.Translate(new Vector3(transform.deltaposition.x - 10,transform.deltaposition.y, transform.deltaposition.z);
I have a better idea. Instead of changing the positions of the buttons, why not change the values that the buttons represent?
Moving Buttons
Remember that the buttons are GameObjects, and every gameobject has a position vector in its transform. So if your button is named ButtonA, then in your code you want to get a reference to that.
GameObject buttonA = GameObject.Find("ButtonA");
//or you can assign the game object from the inspector
Once you have a reference to the button, you can proceed in moving it. So let's imagine that we want to move ButtonA 10 units left.
Vector3 pos = buttonA.transform.position;
pos.X -= 10f;
buttonA.transform.position = pos;

Unity2D - uniform positioning of transforms with different sizes

I have a rectangle (sprite) and I need to place different game objects (sprites) inside that rectangle but so they are all "aligned" by their bottoms.
For the life of me, I cannot make it work in Unity.
Say that my box has a height of 5.
I want to place the different size objects so they are all "resting" at the 2.5 y axis inside the box.
Does anyone know how I can do that since transform.position measures from the center of the GameObject?
Thanks!
Don't use transform.position, use RectTransform properites, as they take anchor points into account. In particular you need to set the anchor position for the sprite in the prebab / inspector and then use RectTransform.anchoredPosition to position it.

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

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.

Is there any reason for u_sprite_size to be (0,0)?

I've been working to add a fragment shader to a SKSpriteNode of size (1024,768) on a screen with a size of (1024, 768), but the variables in the shader keep returning strange values. The u_sprite_size is always (0,0) and the v_tex_coord.x seems to be 0.1435 at one end of the sprite and .16075 at the other.
Is there any reason this should be happening?