I have this script for the character selection menu in my game, but there’s a lot of repeated code. Is there any way I can condense these into, say, one leave function and one join function? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CharacterSelect : MonoBehaviour {
public CanvasGroup p1Canvas, p2Canvas, p3Canvas, p4Canvas;
public Text p1Text, p2Text, p3Text, p4Text;
public int p1Index = 0, p2Index = 0, p3Index = 0, p4Index = 0;
List<string> classes = new List<string>(new string[] { "Archer", "Warrior"});
void Start()
{
p1Leave();
p2Leave();
p3Leave();
p4Leave();
PlayerInfo.players = 0;
}
// LEAVING GAME
public void leaveGame(int controller)
{
if (PlayerInfo.p1Controller == controller) p1Leave();
else if (PlayerInfo.p2Controller == controller) p2Leave();
else if (PlayerInfo.p3Controller == controller) p3Leave();
else if (PlayerInfo.p4Controller == controller) p4Leave();
}
public void p1Leave()
{
p1Canvas = GameObject.Find("Player 1").GetComponent<CanvasGroup>();
p1Canvas.alpha = 0f;
p1Canvas.blocksRaycasts = false;
PlayerInfo.p1Joined = false;
p1Text.text = "Press SPACE/START to join";
PlayerInfo.players--;
PlayerInfo.p1Controller = 0;
PlayerInfo.p1Horizontal = "";
PlayerInfo.p1Vertical = "";
PlayerInfo.p1Fire1 = "";
PlayerInfo.p1Fire2 = "";
PlayerInfo.p1Fire3 = "";
PlayerInfo.p1LookHorizontal = "";
PlayerInfo.p1LookVertical = "";
PlayerInfo.p1Submit = "";
PlayerInfo.p1Cancel = "";
}
public void p2Leave()
{
p2Canvas = GameObject.Find("Player 2").GetComponent<CanvasGroup>();
p2Canvas.alpha = 0f;
p2Canvas.blocksRaycasts = false;
PlayerInfo.p2Joined = false;
p2Text.text = "Press SPACE/START to join";
PlayerInfo.players--;
PlayerInfo.p2Controller = 0;
PlayerInfo.p2Horizontal = "";
PlayerInfo.p2Vertical = "";
PlayerInfo.p2Fire1 = "";
PlayerInfo.p2Fire2 = "";
PlayerInfo.p2Fire3 = "";
PlayerInfo.p2LookHorizontal = "";
PlayerInfo.p2LookVertical = "";
PlayerInfo.p2Submit = "";
PlayerInfo.p2Cancel = "";
}
public void p3Leave()
{
p3Canvas = GameObject.Find("Player 3").GetComponent<CanvasGroup>();
p3Canvas.alpha = 0f;
p3Canvas.blocksRaycasts = false;
PlayerInfo.p3Joined = false;
p3Text.text = "Press SPACE/START to join";
PlayerInfo.players--;
PlayerInfo.p3Controller = 0;
PlayerInfo.p3Horizontal = "";
PlayerInfo.p3Vertical = "";
PlayerInfo.p3Fire1 = "";
PlayerInfo.p3Fire2 = "";
PlayerInfo.p3Fire3 = "";
PlayerInfo.p3LookHorizontal = "";
PlayerInfo.p3LookVertical = "";
PlayerInfo.p3Submit = "";
PlayerInfo.p3Cancel = "";
}
public void p4Leave()
{
p4Canvas = GameObject.Find("Player 4").GetComponent<CanvasGroup>();
p4Canvas.alpha = 0f;
p4Canvas.blocksRaycasts = false;
PlayerInfo.p4Joined = false;
p4Text.text = "Press SPACE/START to join";
PlayerInfo.players--;
PlayerInfo.p4Controller = 0;
PlayerInfo.p4Horizontal = "";
PlayerInfo.p4Vertical = "";
PlayerInfo.p4Fire1 = "";
PlayerInfo.p4Fire2 = "";
PlayerInfo.p4Fire3 = "";
PlayerInfo.p4LookHorizontal = "";
PlayerInfo.p4LookVertical = "";
PlayerInfo.p4Submit = "";
PlayerInfo.p4Cancel = "";
}
//JOINING GAME
public void joinGame(int controller)
{
if (PlayerInfo.p1Joined == false) p1Join(controller);
else if (PlayerInfo.p2Joined == false) p2Join(controller);
else if (PlayerInfo.p3Joined == false) p3Join(controller);
else if (PlayerInfo.p4Joined == false) p4Join(controller);
}
public void p1Join(int controller)
{
PlayerInfo.players++;
PlayerInfo.p1Joined = true;
p1Canvas.alpha = 1f;
p1Canvas.blocksRaycasts = true;
p1Text.text = classes[p1Index];
PlayerInfo.p1Controller = controller;
PlayerInfo.p1Horizontal = "Horizontal_P" + controller;
PlayerInfo.p1Vertical = "Vertical_P" + controller;
PlayerInfo.p1Fire1 = "Fire1_P" + controller;
PlayerInfo.p1Fire2 = "Fire2_P" + controller;
PlayerInfo.p1Fire3 = "Fire3_P" + controller;
PlayerInfo.p1LookHorizontal = "LookHorizontal_P" + controller;
PlayerInfo.p1LookVertical = "LookVertical_P" + controller;
PlayerInfo.p1Submit = "Submit_P" + controller;
PlayerInfo.p1Cancel = "Cancel_P" + controller;
}
public void p2Join(int controller)
{
PlayerInfo.players++;
PlayerInfo.p2Joined = true;
p2Canvas.alpha = 1f;
p2Canvas.blocksRaycasts = true;
p2Text.text = classes[p2Index];
PlayerInfo.p2Controller = controller;
PlayerInfo.p2Horizontal = "Horizontal_P" + controller;
PlayerInfo.p2Vertical = "Vertical_P" + controller;
PlayerInfo.p2Fire1 = "Fire1_P" + controller;
PlayerInfo.p2Fire2 = "Fire2_P" + controller;
PlayerInfo.p2Fire3 = "Fire3_P" + controller;
PlayerInfo.p2LookHorizontal = "LookHorizontal_P" + controller;
PlayerInfo.p2LookVertical = "LookVertical_P" + controller;
PlayerInfo.p2Submit = "Submit_P" + controller;
PlayerInfo.p2Cancel = "Cancel_P" + controller;
}
public void p3Join(int controller)
{
PlayerInfo.players++;
PlayerInfo.p3Joined = true;
p3Canvas.alpha = 1f;
p3Canvas.blocksRaycasts = true;
p3Text.text = classes[p3Index];
PlayerInfo.p3Controller = controller;
PlayerInfo.p3Horizontal = "Horizontal_P" + controller;
PlayerInfo.p3Vertical = "Vertical_P" + controller;
PlayerInfo.p3Fire1 = "Fire1_P" + controller;
PlayerInfo.p3Fire2 = "Fire2_P" + controller;
PlayerInfo.p3Fire3 = "Fire3_P" + controller;
PlayerInfo.p3LookHorizontal = "LookHorizontal_P" + controller;
PlayerInfo.p3LookVertical = "LookVertical_P" + controller;
PlayerInfo.p3Submit = "Submit_P" + controller;
PlayerInfo.p3Cancel = "Cancel_P" + controller;
}
public void p4Join(int controller)
{
PlayerInfo.players++;
PlayerInfo.p4Joined = true;
p4Canvas.alpha = 1f;
p4Canvas.blocksRaycasts = true;
p4Text.text = classes[p4Index];
PlayerInfo.p4Controller = controller;
PlayerInfo.p4Horizontal = "Horizontal_P" + controller;
PlayerInfo.p4Vertical = "Vertical_P" + controller;
PlayerInfo.p4Fire1 = "Fire1_P" + controller;
PlayerInfo.p4Fire2 = "Fire2_P" + controller;
PlayerInfo.p4Fire3 = "Fire3_P" + controller;
PlayerInfo.p4LookHorizontal = "LookHorizontal_P" + controller;
PlayerInfo.p4LookVertical = "LookVertical_P" + controller;
PlayerInfo.p4Submit = "Submit_P" + controller;
PlayerInfo.p4Cancel = "Cancel_P" + controller;
}
// KEY PRESSES
void Update()
{
if (PlayerInfo.p1Joined == true && Input.GetButtonDown(PlayerInfo.p1Submit))
{
print(PlayerInfo.p1Submit);
StartGame();
}
if (Input.GetKeyDown(KeyCode.Space) && PlayerInfo.keyboardAssigned == false)
{
PlayerInfo.keyboardAssigned = true;
joinGame(5);
}
if (Input.GetKeyDown(KeyCode.Escape) && PlayerInfo.keyboardAssigned == true)
{
PlayerInfo.keyboardAssigned = false;
leaveGame(5);
}
if (Input.GetKeyDown(KeyCode.Joystick1Button7) && PlayerInfo.joystick1Assigned == false)
{
PlayerInfo.joystick1Assigned = true;
joinGame(1);
}
if (Input.GetKeyDown(KeyCode.Joystick1Button1) && PlayerInfo.joystick1Assigned == true)
{
PlayerInfo.joystick1Assigned = false;
leaveGame(1);
}
if (Input.GetKeyDown(KeyCode.Joystick2Button7) && PlayerInfo.joystick2Assigned == false)
{
PlayerInfo.joystick2Assigned = true;
joinGame(2);
}
if (Input.GetKeyDown(KeyCode.Joystick2Button1) && PlayerInfo.joystick2Assigned == true)
{
PlayerInfo.joystick2Assigned = false;
leaveGame(2);
}
if (Input.GetKeyDown(KeyCode.Joystick3Button7) && PlayerInfo.joystick3Assigned == false)
{
PlayerInfo.joystick3Assigned = true;
joinGame(3);
}
if (Input.GetKeyDown(KeyCode.Joystick3Button1) && PlayerInfo.joystick3Assigned == true)
{
PlayerInfo.joystick3Assigned = false;
leaveGame(3);
}
if (Input.GetKeyDown(KeyCode.Joystick4Button7) && PlayerInfo.joystick4Assigned == false)
{
PlayerInfo.joystick4Assigned = true;
joinGame(4);
}
if (Input.GetKeyDown(KeyCode.Joystick4Button1) && PlayerInfo.joystick4Assigned == true)
{
PlayerInfo.joystick4Assigned = false;
leaveGame(1);
}
}
public void StartGame()
{
if (PlayerInfo.p1Joined == true) PlayerInfo.p1Class = p1Text.text;
if (PlayerInfo.p2Joined == true) PlayerInfo.p2Class = p2Text.text;
if (PlayerInfo.p3Joined == true) PlayerInfo.p3Class = p3Text.text;
if (PlayerInfo.p4Joined == true) PlayerInfo.p4Class = p4Text.text;
SceneManager.LoadScene("Game");
}
public void Back()
{
SceneManager.LoadScene("Main Menu");
}
}