Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMeshPro/RectTransform GameObject localPosition is being reset

Discussion in 'UGUI & TextMesh Pro' started by rms80, Jun 11, 2018.

  1. rms80

    rms80

    Joined:
    Aug 2, 2015
    Posts:
    8
    I just tried upgrading to unity 2018.1.3 from 2017.1.3p4, and I am having an issue with TextMesh elements where transforms are being cleared. This appears to be due to the attached RectTransform and what appears to be either a bug, or at least very odd behavior?

    I am creating gameObjects in code, attaching TextMeshPro behaviors to them, initializing various layout things, and then positioning them by setting gameObject.transform.localPosition.

    In the past, this has been working without problems. With Unity 2018.1.3, localPosition.x and .y are being set to 0. This is happening both with the old 2017.3.1.0.56 version of TextMeshPro, and with the new Package Manager version.

    These gameObjects are nested inside of parent gameObjects, and they are not in a Canvas/UI layer, they are in-scene 3D text elements.

    I have narrowed the problem down to a very simple test case:

    Code (CSharp):
    1.        
    2.         GameObject textGO = new GameObject("textGO");
    3.         textGO.AddComponent<RectTransform>();
    4.  
    5.         // create parent GO and add textGO as child
    6.         GameObject parentGO = new GameObject("parent");
    7.         textGO.transform.SetParent(parentGO.transform, true);
    8.         parentGO.AddChild(textGO, true);
    9.  
    10.         // now set local position
    11.         textGO.transform.localPosition = new Vector3(100, 100, 10);
    12.         Debug.Log("after set localPosition, textGO localPosition is " + textGO.GetLocalPosition());
    13.  
    14.         // now add parent as child of a second parent
    15.         GameObject parentGOTwo = new GameObject("parentTwo");
    16.         parentGO.transform.SetParent(parentGOTwo.transform, true);
    17.  
    18.         Debug.Log("after adding parent to parentTwo, textGO localPosition is " + textGO.GetLocalPosition());
    19.  
    This code will print:

    Code (csharp):
    1.  
    2. after set localPosition, textGO localPosition is 100.00000000 100.00000000 10.00000000
    3. after adding parent to parentTwo, textGO localPosition is 0.00000000 0.00000000 10.00000000
    4.  
    If I remove the RectTransform component on the initial textGO, it prints

    Code (csharp):
    1.  
    2. after set localPosition, textGO localPosition is 100.00000000 100.00000000 10.00000000
    3. after adding parent to parentTwo, textGO localPosition is 100.00000000 100.00000000 10.00000000
    4.  
    Which I believe should be the correct behavior...both parents are at the origin, in the second one the GameObject stays put but in the first it moves to a different spot depending on parent nesting level...

    Inexplicably, this sample code produces the same behavior in Unity 2017.3. and 2018.1, but in my other code the position-clearing only happens in 2018.1. I suspect that maybe this is related to Monobehavior ordering? It is definitely the source of my problem, if I update the localPosition in every frame, the text is in the right place.

    Is this happening intentionally? Is there a way to enable the old behavior?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Looks like some potential RectTransform issue and not specific to TMP. See if you also get the same behavior in 2018.2.

    I would recommend submitting a bug report on this one, if the behavior is the same in 2018.2.
     
  3. rms80

    rms80

    Joined:
    Aug 2, 2015
    Posts:
    8
    this appears to be fixed in 2018.1.4. Thanks for your reply.
     
    Stephan_B likes this.
  4. reification_bherrera

    reification_bherrera

    Joined:
    Jan 17, 2019
    Posts:
    6
    I'm seeing similar behavior in 2018.4.17, so it looks like it has regressed.
    After creating and setting up TextMeshPro via editor script assigning to transform.position would work, but a subsequent manipulation of the scene was consistently clearing the x and y values of transform position, but only for specific objects.

    Was able to work around the bug by
    1) Setting the localPosition once during object creation and setup with no later modifications
    2) Calling ForceUpdateRectTransforms() immediately after the assignment to localPosition
     
    Last edited: Mar 2, 2020