I’m adding a textmeshpro to the canvas and it looks fine, but if I leave the scene or press play on the game the text disappears and I’m getting this error.
After that the text component looks like this:
How can I fix this? I couldn’t find any information about this problem online
Sounds like you have a defective singleton (aka, "some script using DontDestroyOnLoad() inappropriately), perhaps one that is outliving the things it is referencing. That’s my first guess in any case.
The answer is always the same… ALWAYS!
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
Identify what is null ← any other action taken before this step is WASTED TIME
Identify why it is null
Fix that
Beyond that, here’s how to make a proper singleton:
Simple Singleton (UnitySingleton):
Some super-simple Singleton examples to take and modify:
Simple Unity3D Singleton (no predefined data):
SingletonSimple.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// by @kurtdekker - to make a simple Unity singleton that has no
// predefined data associated with it, eg, a high score manager.
//
// To use: access with SingletonSimple.Instance
//
// To set up:
This file has been truncated. show original
Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:
SingletonViaPrefab.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// by @kurtdekker - to make a Unity singleton that has some
// prefab-stored, data associated with it, eg a music manager
//
// To use: access with SingletonViaPrefab.Instance
//
// To set up:
This file has been truncated. show original
These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance