Accessing an object from another scene

My game requires the changing of scenes. My game also requires the health to stay the same and not get destroyed in the transition of the scene. Which I did with the use of DontDestroyOnload. In the second scene, I want one of the game object’s script to use the health bar(which is in the first scene and using DontDestroyOnload). how do I do that?

We normally put an object called “Persistent” in the root of the first scene. Has a script on it only doing this:

    public class PersistentGameObject : MonoBehaviour
    {
        void Awake()
        {
            DontDestroyOnLoad(gameObject);
        }
    }

Then you can place a number of GameObjects under that Persistent object (for instance one with a script called HealthStatus), and they will all survive the scene change so you can access them later. You will need to find them after going to the second scene though, in however way you normally do that. Say:

FindObjectOfType<HealthStatus>() 

or singleton pattern or however you feel like.

(but don’t do a FindObjectOfType in each frame ofc)