/* * This method exchanges the Child of the GameObject the script is attached to (Player1 or Player2) with * an instance of the prefab that has been set in the classes-array in the Unity inspector. * The classes get changed according to their index in the array (0 - first). * It afterwards sets the new Player-GameObject as the new target of the camera. */ private IEnumerator ChangeClass(bool changedDownwards) { // The transform of the player gets saved, afterwards the old Player-Gameobject gets destroyed Transform newPlayerTransform = playerObject.transform; Destroy(playerObject.gameObject); // The player switches his class from fairy to the next class if (classes[currentClassIndex].name == "fairy") { hasFairy = false; } // Increments the Index UpdateCurrentClassIndex(changedDownwards); // This prohibits both players from being a fairy at the same time if (classes[currentClassIndex].name == "fairy" && (hasFairy || otherPlayer.isDead)) { UpdateCurrentClassIndex(changedDownwards); } // Instantiates the new GameObject playerObject = Instantiate(classes[currentClassIndex], newPlayerTransform.position, newPlayerTransform.rotation, gameObject.transform) as GameObject; SetAxis(); // The player now is a fairy if (classes[currentClassIndex].name == "fairy") { hasFairy = true; } // Sets the target for the fairy SetFairyTarget(); playerObject.GetComponentInChildren <MovingObj>()._hitpoints = PlayerPrefs.GetInt("HP" + gameObject.name); // Setting the new player as target of the camera cameraControl.SetTarget(playerNumber - 1, playerObject); // Change the portrait to fit the new class UpdateClassUI(); cdmanager.StartChangeClassCD(); yield return(new WaitForSeconds(0.25f)); Subject.Notify("Player changed class"); Subject.Notify("Player changed class", currentClassIndex); }