Why is My Game Object Vanishing when Running the Game?

While working on the 2D RPG I’ve kind of stumbled into a solution for making the party system work (small yay). This unfortunately has led to a weird issue where one of the characters will disappear as soon as I start the game. I don’t mean like his renderer is turned off or he is flung into the great abyss that exists outside a running game. I mean he just straight up vanishes from Unity all together. In the attached pictures you can see the player character in the middle of the other two characters. The images show that both characters have identical components and the third picture shows that when the game is running, the one on the left is just…gone. When I turn off play mode he magically reappears.

I have determined that the cause is apparently the Companion Script I have also included which controls them being turned off and being added to a Party component on the Player character. I can go in and turn off the script component on the one who disappears and run the game and he doesn’t vanish so that tells me it’s the code, but I can’t see anything in the code that causes him to vanish like he does. I’ve deleted their prefabs, removed all components, replaced them as brand new, it’s a rinse and repeat of one of them disappearing. Sometimes it’s the one of the left, other times it’s the one on the right who vanishes.

public class CompanionManager : MonoBehaviour
{

    public static CompanionManager instance;

    [SerializeField] GameObject partyChoicePanel;

    public GameObject child;
    public Transform parent;

    // Start is called before the first frame update
    void Start()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void OpenPartyChoicePanel()
    {
        if (GameObject.FindGameObjectWithTag("Companion"))
        {
            Debug.Log("Found an ally");
            partyChoicePanel.gameObject.SetActive(true);
        }
    }

    public void ClosePartyChoicePanel()
    {       
        partyChoicePanel.gameObject.SetActive(false);
    }

    public void JoinParty()
    {
        Debug.Log("Added to party");
        AddPartyMember(parent);
        partyChoicePanel.gameObject.SetActive(false);

        if (GameObject.FindGameObjectWithTag("Companion"))
        {
            gameObject.GetComponent<SpriteRenderer>().enabled = false;
            gameObject.GetComponent<BoxCollider2D>().enabled = false;
        }
    }

    public void AddPartyMember(Transform newParent)
    {
        child.transform.parent = newParent;
    }
}

In general, I’d recommend learning the basics on how to debug code. Either by playing breakpoints in your code, and stepping through the code with a debugger attached, or by at least adding Debug.Log statements in your code, and looking at the Console output to see whether you’re reaching certain lines of code.

In this case, though, the problems seems pretty clear: You’re adding more than one “CompanionManager” to your scene, but your CompanionManager code assumes that only one CompanionManager will ever exist at a time. The reason is, you’re using a “static” reference to the instance: public static CompanionManager instance;. By making the “instance” static, that means that only a single instance can exist, for any number of CompanionManager components added to any number of game objects. You only want to use that approach if you’re following the Singleton pattern. But in this case, you clearly add one CompanionManager to each companion. So what’s happening is that every time Start runs in CompanionManager, it checks if “instance” is null. The first time it won’t be, and everything will be fine. But for every subsequence time, it will destroy the previous “instance”.

It doesn’t look like you want to be making this a singleton. Certainly not if you’re adding this component to individual character.