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

Error: There is no 'CanvasRenderer' attached to the "Caret" game object

Discussion in 'UGUI & TextMesh Pro' started by MaltedWheaties, Jun 2, 2020.

  1. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
    Hi there,
    I am trying to use a TextMeshPro input field, and I get an error:

    MissingComponentException: There is no 'CanvasRenderer' attached to the "Caret" game object, but a script is trying to access it.
    You probably need to add a CanvasRenderer to the game object "Caret". Or your script needs to check if the component is attached before using it.

    I am not using any scripts, apart from a simple delay text script for a separate TMP text object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Terminal : MonoBehaviour
    8. {
    9.     TextMeshProUGUI txt;
    10.     string story;
    11.  
    12.     [SerializeField]
    13.     private float delay = 0.125f;
    14.  
    15.     void Awake()
    16.     {
    17.         txt = GetComponent<TextMeshProUGUI>();
    18.         story = txt.text;
    19.         txt.text = "";
    20.  
    21.         // TODO: add optional delay when to start
    22.         StartCoroutine("PlayText");
    23.     }
    24.  
    25.     IEnumerator PlayText()
    26.     {
    27.         foreach (char c in story)
    28.         {
    29.             txt.text += c;
    30.             yield return new WaitForSeconds(delay);
    31.         }
    32.     }
    33. }
    I don't think that's the problem. The error only occurs when I add the field.

    Should I reimport TMP? I tried deleting the folder from Assets, and reinstalling it, but the same thing happened.

    Thanks for your help ;)
     
  2. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    I'm having the same problem. :(
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Make sure you are using the correct version of the TMP package for the version of Unity you are using.

    Version 1.5.0 for Unity 2018.4
    Version 2.1.0 for Unity 2019.x
    Version 3.0.0 for Unity 2020.x

    Make sure you re-import the TMP Essential Resources as these have been updated.
     
  4. The_Biscuit

    The_Biscuit

    Joined:
    Dec 29, 2018
    Posts:
    1
    Thank you, saved me quite some messing around.
     
  5. MansyrevAY

    MansyrevAY

    Joined:
    Nov 10, 2016
    Posts:
    14
    Still having this problem even after reimporting the TMP
     
  6. Stepan323446

    Stepan323446

    Joined:
    Jun 8, 2020
    Posts:
    7
    The problem was resolved in update 3.0.1
     
  7. MansyrevAY

    MansyrevAY

    Joined:
    Nov 10, 2016
    Posts:
    14
    Im using Unity 2020.1, and latest version I can get in Package Manager is 3.0.0-preview.1. How can I get 3.0.1?
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Make sure you have either Unity Registry or Packages in Project selected.

    upload_2020-8-13_15-31-45.png
     
    mikewar123 likes this.
  9. unity_vcO5J1NAui1rbw

    unity_vcO5J1NAui1rbw

    Joined:
    Jul 1, 2019
    Posts:
    2
    I am currently on Unity 2020.1.f3 I am using TMP 3.0.1 and still having the issue. added full error message from console.

    MissingComponentException: There is no 'CanvasRenderer' attached to the 'IconMission' game object, but a script is trying to access it. You probably need to add a CanvasRenderer to the game object 'IconMission'. Or your script needs to check if the component is attached before using it. UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.Canvas canvas, UnityEngine.Camera eventCamera, UnityEngine.Vector2 pointerPosition, System.Collections.Generic.IList`1[T] foundGraphics, System.Collections.Generic.List`1[T] results) (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/GraphicRaycaster.cs:321) UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] resultAppendList) (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/GraphicRaycaster.cs:225) UnityEngine.EventSystems.EventSystem.RaycastAll (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] raycastResults) (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:255) UnityEngine.EventSystems.PointerInputModule.GetMousePointerEventData (System.Int32 id) (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/PointerInputModule.cs:277) UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent (System.Int32 id) (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:541) UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent () (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:527) UnityEngine.EventSystems.StandaloneInputModule.Process () (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:282) UnityEngine.EventSystems.EventSystem.Update () (at /Applications/Unity/Hub/Editor/2020.1.3f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The above stack trace, does not make any references to TMP so the error is unlikely related to TMP.

    This stack trace references an object by the name of IconMission which appears to use Canvas / UGUI functionality.

    Does this object have a text component on it?

    What type of components are on this object?
     
  11. unity_vcO5J1NAui1rbw

    unity_vcO5J1NAui1rbw

    Joined:
    Jul 1, 2019
    Posts:
    2
    there is a custom empty graphics

    Code (CSharp):
    1. public class Touchable : Graphic, ICanvasRaycastFilter
    2.     {
    3.         public new void OnEnable()
    4.         {
    5.             base.OnEnable();
    6.             base.color = Color.clear;
    7.         }
    8.  
    9.         public virtual bool IsInteractable()
    10.         {
    11.             return true;
    12.         }
    13.  
    14.         public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    15.         {
    16.             return true;
    17.         }
    18.  
    19.         protected override void OnPopulateMesh(VertexHelper vh)
    20.         {
    21.             // Dispose some random invisible geometry
    22.             vh.Clear();
    23.         }
    24.     }
    and custom button
    Code (CSharp):
    1.  public class CustomButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
    2.     {
    3.      ....
    4.     }
    there is a child object under it that has wrapper support for TextMeshProUGUI
    Code (CSharp):
    1.  
    2.         public void SetLabel(string label, TextFieldID fieldID = TextFieldID.Main)
    3.         {
    4.             if (m_labels == null || Application.isPlaying == false)
    5.                 m_labels = this.GetComponentsInChildren<Text>(true);
    6.  
    7.             foreach (Text rt in m_labels)
    8.                 rt?.SetLabel(label, fieldID);
    9.         }
    the stack trace is from GO with no actual child Text on it, but result is the same if there is an actual Text underneath
     
  12. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    If your parent objects is implementing functionality that requires a Canvas such as inheriting from the Graphic class, this object needs a CanvasRenderer on it.

    In prior releases of Unity and unrelated to TMP, the Graphic class used to add a CanvasRenderer to all objects inheriting from Graphic whether or not those object needed a CanvasRenderer.

    To resolve your issue and since you are using functionality from the Graphic class that requires a CanvasRenderer, you can add the following attribute to your Touchable class.

    [RequireComponent(typeof(CanvasRenderer))]
     
    sejton likes this.
  13. krisventure

    krisventure

    Joined:
    Mar 24, 2016
    Posts:
    118
    Careful when "updating" TMP in package manager though, I just did it and it's triggered full re-import of the entire Unity project, apparently bug reported by others too. Make sure you check any other alternatives before updating TMP, or make sure you can afford the time it takes to re-import your project (depending on your project size it can easily take 5+ hours).
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This behavior is the result of the removal of an AssetPostProcessor which triggers the re-import of textures.

    BTW: Upgrading to newer major versions of Unity also triggers this.

    I have updated the default package of TMP for 2020.x or newer to version 3.0.6 where upgrading to a new major version of Unity (for instance 2019.4 to 2020.3) will result in the re-import because of the major version change but will not re-import the textures since the TMP package will be 3.0.6. Basically, the re-imports get combined.