public void OnCharacterSelect(int characterChoice) { abilityPanel.SetActive(true); Character selectedCharacter = characters [characterChoice]; /* Destroy current character and instantiate new character */ if (CharacterControl.instance != null && CharacterControl.instance.player != null) { Transform t = CharacterControl.instance.player.transform; Vector2 velocity = CharacterControl.instance.player.GetComponent <Rigidbody2D>().velocity; Destroy(CharacterControl.instance.player); if (characterChoice == 1) { Transform tmp = t; tmp.transform.position = new Vector2(tmp.transform.position.x, tmp.transform.position.y); curChar = 1; CharacterControl.instance.player = Instantiate(selectedCharacter.prefab, tmp.transform.position, t.transform.rotation); } else if (characterChoice == 0 && curChar == 1) { Transform tmp = t; tmp.transform.position = new Vector2(tmp.transform.position.x, tmp.transform.position.y); curChar = 0; CharacterControl.instance.player = Instantiate(selectedCharacter.prefab, tmp.transform.position, t.transform.rotation); } else { CharacterControl.instance.player = Instantiate(selectedCharacter.prefab, t.transform.position, t.transform.rotation); curChar = characterChoice; } CharacterControl.instance.player.GetComponent <Rigidbody2D>().velocity = velocity; } else { CharacterControl.instance.player = Instantiate(selectedCharacter.prefab); } CharacterControl.instance.jump_velocity = selectedCharacter.jump_velocity; CharacterControl.instance.characterNumber = characterChoice; FindObjectOfType <AudioManager>().Play("Transform"); if (characterPanel != null) { characterPanel.SendMessage("SwapCharacter", characterChoice); } WeaponMarker weaponMarker = CharacterControl.instance.player.GetComponentInChildren <WeaponMarker> (); AbilityCoolDown[] coolDownButtons = GetComponentsInChildren <AbilityCoolDown> (); for (int i = 0; i < coolDownButtons.Length; i++) { coolDownButtons [i].Initialize(selectedCharacter.characterAbilities [i], weaponMarker.gameObject); } }
public void OnCharacterSelect(int characterChoice) { characterSelectPanel.SetActive(false); abilityPanel.SetActive(true); GameObject spawnedPlayer = Instantiate(player, playerSpawnPosition, Quaternion.identity) as GameObject; WeaponMarker weaponMarker = spawnedPlayer.GetComponentInChildren <WeaponMarker> (); AbilityCooldown[] cooldownButtons = GetComponentsInChildren <AbilityCooldown> (); Character selectedCharacter = characters [characterChoice]; for (int i = 0; i < cooldownButtons.Length; i++) { cooldownButtons[i].Initialize(selectedCharacter.characterAbilities[i], weaponMarker.gameObject); } }
public void OnCharacterSelect(int characterChoice) //function called when the button is pressed //parameter is passed by the button that was clicked { characterActive = true; playerHealthBarManager.healthBar.gameObject.SetActive(true); playerHealthBarManager.healthText.gameObject.SetActive(true); characterSelectPanel.SetActive(false); //hidden when first called abilityPanel.SetActive(true); //activated when first called timerText.gameObject.SetActive(true); if (characterChoice == 0) { spawnedPlayer = Instantiate(warrior, playerSpawnPosition, Quaternion.identity) as GameObject; //casted as a GameObject //Quaternion.identity returns the rotation of the original prefab } else if (characterChoice == 1) { spawnedPlayer = Instantiate(archer, playerSpawnPosition, Quaternion.identity) as GameObject; } else { spawnedPlayer = Instantiate(mage, playerSpawnPosition, Quaternion.identity) as GameObject; } WeaponMarker weaponMarker = spawnedPlayer.GetComponentInChildren <WeaponMarker>(); //search (starting from the spawnedPlayer object) down the heirachy until it finds a weaponMarker component attached to a game object //used to find the weapon marker script in the heirarchy AbilityCooldown[] cooldownButtons = GetComponentsInChildren <AbilityCooldown>(); //look through the children attached to the game object this script is attached to and search down the heirachy and store ANY ability cooldown scripts it finds //then stored in this array Character selectedCharacter = characters[characterChoice]; //character is selected based on which button the player picked (such as 0) //then gets the index character of the array (spot 0 in that case) for (int i = 0; i < cooldownButtons.Length; i++) //loops through the array until all have been initialzed { //*IMPORTANT* If there are too many abilities, then some will get ignored because there are too many abilities //too few abilities will create blank buttons //need to account for this try //set up just in case there are too many abilities or ability icons and an error is thrown { cooldownButtons[i].Initialize(selectedCharacter.characterAbilities[i], weaponMarker.gameObject); //each character has an array of character abilities //the selected ability is the one in character abilities spot i //the weapon holder is the first object that was found that had a weapon marker } catch { //pass because abilities or ability icons are out of range } } }