Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why is My Game Object Vanishing when Running the Game?

Discussion in 'Editor & General Support' started by Draugrlord, Dec 2, 2023.

  1. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    29
    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.

    Code (CSharp):
    1. public class CompanionManager : MonoBehaviour
    2. {
    3.  
    4.     public static CompanionManager instance;
    5.  
    6.     [SerializeField] GameObject partyChoicePanel;
    7.  
    8.     public GameObject child;
    9.     public Transform parent;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         if (instance != null && instance != this)
    15.         {
    16.             Destroy(this.gameObject);
    17.         }
    18.         else
    19.         {
    20.             instance = this;
    21.         }
    22.  
    23.         DontDestroyOnLoad(gameObject);
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.        
    30.     }
    31.  
    32.     public void OpenPartyChoicePanel()
    33.     {
    34.         if (GameObject.FindGameObjectWithTag("Companion"))
    35.         {
    36.             Debug.Log("Found an ally");
    37.             partyChoicePanel.gameObject.SetActive(true);
    38.         }
    39.     }
    40.  
    41.     public void ClosePartyChoicePanel()
    42.     {      
    43.         partyChoicePanel.gameObject.SetActive(false);
    44.     }
    45.  
    46.     public void JoinParty()
    47.     {
    48.         Debug.Log("Added to party");
    49.         AddPartyMember(parent);
    50.         partyChoicePanel.gameObject.SetActive(false);
    51.  
    52.         if (GameObject.FindGameObjectWithTag("Companion"))
    53.         {
    54.             gameObject.GetComponent<SpriteRenderer>().enabled = false;
    55.             gameObject.GetComponent<BoxCollider2D>().enabled = false;
    56.         }
    57.     }
    58.  
    59.     public void AddPartyMember(Transform newParent)
    60.     {
    61.         child.transform.parent = newParent;
    62.     }
    63. }
    Nodrig.png Rudac.png Vanishing Character.png
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    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.