Text alignment not working
I am creating a game where I instantiate a prefab with a text component into a child of a canvas, and then edit the text variable. When I do so, the text doesn't appear where it should relative to its rectTransform and text alignment. Changing the alignment has no affect, although I can change the font and colour. I can also change the position with the rTrans position and pivot, as well as scale and rotation. This prefab works in other canvases that I have in the scene at the same time, but the troublesome canvas is the only one in Screen Space (Cam) render mode. If I add a prefab before i play it, it works properly. I use two different prefabs with different pivots, and text alignments only; both don't work. I have already implemented the function over a year ago, and it has worked as intended. It is only recently, when i was working on what I am pretty sure is unrelated, that this problem started, and I don't know what the cause is. There are no other scripts attached to the texts other than what is on the image. There is a parent that changes scale but I didn't think it would cause the error as it has worked while using it.
Answer by XtrainfiNIGHT · Jul 10, 2017 at 02:49 AM
I figured it out. In the texts' parent gameobject, I have a script that automatically changes scale to make sure that the puzzle stays on screen in the right position. Every Update frame it would calculate what scale it should be and then go: rectTransform.localScale = Vector3.one x scale; However, I was editing the script to take into account some side options and I ended making it rectTransform.localScale = Vector2.one x scale; (I think) this makes the z scale axis 0f, which caused the error. and if it didn't my game now works anyway.
I used "x" to symbolise the multiply symbol because it is in these text boxes to change style.
Answer by UnityCoach · Jul 09, 2017 at 02:28 PM
I see position at x:25.86, y:76.1. You probably want to reset this to 0, 0, as your using a fixed anchor, the position behaves as a delta between parent and child pivot points.
There were many text objects near each other so I moved the one in the picture so it was isolated. The anchor and pivots are used for easy positioning as I instantiate the objects in a grid. The problem is that the visual text doesn't change position according to the text alignment (which is important so as the off center text is easily noticeable in a grid. I know that what I am doing can work because it has for about a year.
I understand, but did you try resetting the position to 0, 0 ?
Yes. The position shouldn't be affecting it though, because both the text, and the sprites are all in the same parent.
Your answer
Follow this Question
Related Questions
Unity UI, Text glitch. 0 Answers
UTF-8 UI Text problem 2 Answers
Changing the UI Text 0 Answers
[SOLVED]My UI text don't update from script 3 Answers
How do I change UI button background AND button text on click(pressed)? 0 Answers