,Having an issue accessing a destroyed object, can't figure out where the problem is coming from?

I’ve been trying to solve this for 2 days now and I can’t figure it out. I have a buy menu (similar to csgo/valorant) that players can click and buy weapons. It works great, but if you get killed and you try to click a button to buy a weapon after you respawn, it gives me the error “MissingReferenceException: The object of type ‘Player’ has been destroyed but you are still trying to access it.”

I’ll throw some code in (all on player script) for context and hope someone can help me out!

Initializes Buy Menu and sets up the buttons, called on Awake.

        private IEnumerator InitBuyMenu()

                GameObject buttonObj = GameObject.Find("BuyMenu/Buttons");
                Button[] buyButton = buttonObj.GetComponentsInChildren<Button>();

                for (int i = 0; i < buyButton.Length; i++)
                    int temp = i;
                    buyButton*.onClick.AddListener(() => BuyPress(temp));*


Debug.Log(“passed buymenu initialize”);

yield return null;

The button press function that is called from the ienumerator
void BuyPress(int ind)
if(gameObject != null)
Weapon weaponController = GetComponent();
Debug.LogWarning(“passed buypress command”);

GunBuyButton.instance.BuyPress(weaponController, ind);

Gives me the error from the if(gameObject != null), obviously because the error says the gameObject (player) doesn’t exist? I know what the issue is I guess? I just don’t understand how to fix it.
If I had a guess, it’s because it’s attempting to still access the OLD player, and not the respawned clone prefab. I’m just not sure how to fix that?

If I did not understand wrong, you are saying that when the player dies, you destroy its instance and for respawning you instantiate a new object. Could you provide the code of those two events ? Also, if you call a Debug.log at the beginning of the BuyPress function, is it called twice ?

It looks like you’re using a singleton pattern with ‘instance’. Static variables persist between scenes.

The reason it’s happening twice is when you reload the scene, you now have two (or more) managers, the one you made before the scene change, and the one you make every time the scene loads.

You need to handle it with an if(instance==null)…else{Destroy gameObject} type statement when you initialise the singleton in Start or Awake to auto-destroy any duplicates.