I'm not entirely sure if it's a bug, but your implementation seems fine.
If Unity really is making a duplicate object, the workaround is to get all the objects with the player tag, and search them for the valid player object.
GameObject[] playerList = GameObject.FindGameObjectsWithTag("Player"); // Get a list of all the objets with the player tag into an array
for (int i = 0; i < playerList.Length(); i++)
{
if (playerList[i].GetComponent() != null) // See if we can find the script on the returned object
{
player = PlayerList[i];
break;
}
}
if (player == null) { // Check to make sure we found the player.
Debug.LogError("Unable to find player object!");
return; // Prevent any NullExceptionErrors
}
↧