Author Topic: Input Caret GameObject having Transform instead of RectTransform on 5.5.2p2  (Read 6365 times)

Korindian

  • Newbie
  • *
  • Posts: 9
Hi,

I didn't have this problem last night, so I'm guessing it happened after installing the latest Unity patch 5.5.2p2.

I'm loading an TMP_InputField GameObject via code into a canvas. After it is parented, I get the error:

Can't add a 'RectTransform' to the "InputField Input Caret" game object during Awake when a 'Transform' is already attached.
You probably need to add a 'RectTransform' to the "InputField Input Caret" game object in the editor.

I notice that the dynamically created InputField Input Caret GameObject (normally called TexteshPro - InputField Input Caret) gets a Transform component rather than a RectTransform when it is loaded via code. If the InputField GameObject already exists in the canvas before pressing play, the Input Caret GameObject correctly gets a RectTransform component.

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
I am traveling until Friday and currently have a pretty bad internet connection which precludes me from installing Unity 5.5.2p2. I should be able to look into this over the weekend. Right now there are RectTransform issues with Unity 5.6 which have yet to get resolved so I am hoping these are not filtering down to Unity 5.5.2...

Unless you absolutely need to use Unity 5.5.2p2, I suggest you stick to Unity 5.5.2p1 which works fine.

mh114

  • Beta User
  • Jr. Member
  • *
  • Posts: 54
I was bitten by this as well, just noticed this error after having upgraded to 5.5.2p2 just a few days earlier.. I guess there's no workaround on TMPs end, looks like a bad Unity bug to me?  :-\

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
I was bitten by this as well, just noticed this error after having upgraded to 5.5.2p2 just a few days earlier.. I guess there's no workaround on TMPs end, looks like a bad Unity bug to me?  :-\

I tried Unity 5.5.2p2 and did't get the error so I might need a Report of this? Are you getting this error when loading the example scene with the Input Field and Scrollbar?

sevensails

  • Newbie
  • *
  • Posts: 18
I have the same problem. Just add a TextMeshPro Input Field and when Testing It the Bug Happens. I'm on Windows.
« Last Edit: March 10, 2017, 11:57:41 AM by sevensails »

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
I have the same problem. Just add a TextMeshPro Input Field and when Testing It the Bug Happens. I'm on Windows.

I just installed Unity 5.5.2p2 on the MacBook Pro which I use when traveling but I am not getting this error.

Can you provide the steps to reproduce the behavior?

sevensails

  • Newbie
  • *
  • Posts: 18
I think I know why you was not able to reproduce it.

Please, try keep the Parent GameObject where the Input Field Is Disabled and Activate it only in Runtime. It seems the problem only happens on this situation.

mh114

  • Beta User
  • Jr. Member
  • *
  • Posts: 54
Yes, it is definitely related to the fact that stuff is disabled on the scene and enabled at runtime.

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
Yes, it is definitely related to the fact that stuff is disabled on the scene and enabled at runtime.

Good information. I got back around 1:00 AM last night but will be looking at this today and provide an update as soon as I have something.

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
In Unity 5.5.2p2, the following now results in the following error: "Can't add a 'RectTransform' to the "New Child" game object during Awake when a 'Transform' is already attached."

Here is the scene hierarchy used to reproduce this behavior.



Here is the script attached to this parent object.

Code: C#
  1. using UnityEngine;
  2.  
  3. public class SandBox_01 : MonoBehaviour {
  4.  
  5.    private GameObject m_ChildGameObject;
  6.  
  7.    private void Awake()
  8.    {
  9.        if (m_ChildGameObject == null)
  10.        {
  11.            m_ChildGameObject = new GameObject("New Child", typeof(RectTransform));
  12.  
  13.            m_ChildGameObject.transform.SetParent(this.transform);
  14.        }
  15.    }
  16. }
  17.  

Key to reproducing the issue is to have the parent object disabled when entering play mode and then enabling it afterwards.

So basically, adding a RectTransform to any object in Awake() at run time, will throw this error in Unity 5.5.2p2 and Unity 5.6. I am not sure of the implication of this new limitation but this seems like an issue to me.

mh114

  • Beta User
  • Jr. Member
  • *
  • Posts: 54
Can you raise an Unity bug about this, Stephan? Sounds like something they should fix.

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
Can you raise an Unity bug about this, Stephan? Sounds like something they should fix.

I will be discussing this tomorrow with the lead on the UI team at Unity.

For certain, not being able to create a gameobject in Awake() and then add any Graphic component to it like an Image seems wrong to me.

mh114

  • Beta User
  • Jr. Member
  • *
  • Posts: 54
I will be discussing this tomorrow with the lead on the UI team at Unity.

For certain, not being able to create a gameobject in Awake() and then add any Graphic component to it like an Image seems wrong to me.

Great! Keep us posted.  :)

avee

  • Newbie
  • *
  • Posts: 8
We've managed to do a quick and dirty fix until the issue is properly resolved.

So we established that Unity, after the update to 5.5.2p2, forbids creation of gameobjects with RectTransform, if the object is instantiated at runtime.

Line 763 of TMP_InputField creates a GameObject named: transform.name + " Input Caret". Its default Transform was silently upgraded to RectTransform, but this is no longer possible.

 A way to fix it is to clone the object's parent which has a RectTransform and a RectMask2D component. Then destroy the RectMask2D component (which is not needed on the caret), destroy its children and rename it to input caret.
It's as if you needed a carton box, but instead of getting just the carton box, bought a box full of bananas, chuck all the bananas out and be happy to have the box you need.

This code below should replace the code from line 763 in TMP_InputField.cs:

Code: [Select]
GameObject go = Instantiate(m_TextComponent.transform.parent.gameObject, m_TextComponent.transform.parent);
                    Destroy(go.GetComponent<RectMask2D>());                   
                    int count = go.transform.childCount;
                    for (int i = count - 1; i >= 0; i--)
                    {
                        GameObject.Destroy(go.transform.GetChild(i).gameObject);
                    }

                    go.name = transform.name + " Input Caret";

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5687
Just heard from the Unity developer(s) responsible for this part of their code and they will be revising the handling of adding components like RectTransforms in Awake.

Long story short, I will be able to modify the current Caret object creation by simply using the following where the RectTransform is added as the object is created.

Code: C#
  1. GameObject go = new GameObject(transform.name + " Input Caret", typeof(RectTransform));
  2.  

Note: Creating a new object and then adding a RectTransform using AddComponent() will no longer be possible in Awake(). But as shown above, creating a new object and adding the RectTransform in that same function will still work in Awake(). This last part isn't really related to TMP but just good overall stuff to know in Unity.