Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TextMeshPro/RectTransform GameObject localPosition is being reset

Discussion in 'Unity UI & 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

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,277
    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.