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;
}
}