I need help diagnosing the following error message:
*MissingReferenceException: The object of type 'Material' has been destroyed but you are still trying to access it.*
*Your script should either check if it is null or you should not destroy the object.*
*UnityEngine.Object.get_name () (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:208)*
*TMPro.TMP_SubMeshUI.AddSubTextObject (TMPro.TextMeshProUGUI textComponent, TMPro.MaterialReference materialReference) (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_SubMeshUI.cs:215)*
*TMPro.TextMeshProUGUI.SetArraySizes (TMPro.TMP_Text+UnicodeChar[] unicodeChars) (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMPro_UGUI_Private.cs:1403)*
*TMPro.TMP_Text.ParseInputText () (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_Text.cs:1902)*
*TMPro.TextMeshProUGUI.OnPreRenderCanvas () (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMPro_UGUI_Private.cs:1644)*
*TMPro.TextMeshProUGUI.Rebuild (UnityEngine.UI.CanvasUpdate update) (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TextMeshProUGUI.cs:216)*
*UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/CanvasUpdateRegistry.cs:215)*
*UnityEngine.Canvas:SendWillRenderCanvases() (at /Users/bokken/build/output/unity/unity/Modules/UI/ScriptBindings/UICanvas.bindings.cs:104)*
For context, I am trying to make a menu for my game. I did this by using the dropdown on the object hierarchy window to create UI
→ Button - TextMeshPro
. After starting with the “Basic 2D” scene template, and adding two buttons to the scene in the aforementioned way, I accrued the following object hierarchy:
However, since there are certain configurations/styles I want to have consistent across each of several buttons I plan to include in the menu, I decided to store one of the button objects as a prefab, and then replace the two button instances in the scene with their prefab. This was roughly the point at which the error message occurred. Interestingly, the error does not occur at runtime. Only in the editor itself. However, it prevents my scene from rendering properly, which makes the design stage a pain.
Is there anything I can do to resolve this error message? I would prefer to use a prefab for my buttons if possible, but if that’s a bad practice for buttons or fundamentally causing the error, please let me know what I can do differently!
Thanks for the help!
UPDATE:
Since I also attempted to make this button with a custom font that I created, I decided to temporarily revert back to a built-in font in case my font was causing the issue. Turns out it was, which should narrow down the search. I noticed when opening the Text (TMP)
object in the Inspector, (this is with my custom font), that at the bottom, under debug settings there is some info about the font that indicates the texture is assigned None (Texture 2D)
Could this be the null object the error message is referencing?
If so, how would I go about fixing this? I don’t know much about textures, and the ones available to me under assets don’t make much sense to me. Also, debug settings only allows me to temporarily change things at runtime, so how would I fix up the prefab permanently?
Finally, another weird side effect is that at runtime, and only when using my custom font, the Text (TMP)
object dynamically receives a child object called"TMP SubMeshUI [LiberationSans SDF Material]"
. No idea what that means, why it receives it, or what to do with it.
UPDATE:
I discovered that after I quit the game and return to the editor (which generates the error, and makes my text not render), if I then expand my custom font asset, and select the material, the “Face” has a “Texture” property. Currently, this value is set to a desirable “None (Texture 2d)”. Interestingly, though, if I hit select, and then reselect this same texture, by the time I return to the scene view, my text renders itself again. If I have to manually reselect something to make it work in the editor, is that an indication that there is a bug with the editor?
IMPORTANT UPDATE:
I accidentally discovered that the issue only occurs when my TMP includes a space in it! Upon further investigation, it appears that this glyph was not included in my character table when I observe the font asset in the inspector. In fact, the tool I used to create the font (BitFontMaker2) does not appear to have a way of defining a glyph for the space character. (Perhaps because it is understood that no pixels would be filled in for this glyph in any font). However, I need to define this glyph somehow, and if BitFontMaker2 does not allow me to do this, is there a way to patch up this asset appropriately in Unity? Or should I use a different font creation tool?