Unknown problem

I have 2 players, with the same script attached to them. P1 with ScriptA and P2 with ScriptA. One of the function is highlight a player on tap, then if player is highlighted, enable pass.

This is how my code looks like.

else if (gesture.pickObject != null)
		{
			if (gesture.pickObject.transform == soccerBall.GetComponent<BallBehaviourCS>().myOwner)
			{
				if (soccerBall.GetComponent<BallBehaviourCS>().myOwner == players [0])
				{
					players[0].GetComponent <TPCCS>().highlight = true;
					players[1].GetComponent <TPCCS>().highlight = false;
				}
				else if (soccerBall.GetComponent<BallBehaviourCS>().myOwner == players [1])
				{
					players[1].GetComponent <TPCCS>().highlight = true;
					players[0].GetComponent <TPCCS>().highlight = false;
				}
			}
			else if (gesture.pickObject.transform != soccerBall.GetComponent<BallBehaviourCS>().myOwner && gesture.pickObject.gameObject.tag == "Player")
			{
				Debug.Log ("Player 2: " + players[1].GetComponent <TPCCS>().highlight);
				if (gesture.pickObject.transform.GetComponent<TPCCS>().highlight == true)
				{
					pass (gesture);
				}
				else
				{
					gesture.pickObject.transform.GetComponent <TPCCS>().highlight = true;
					if(gesture.pickObject.transform == players[0])
					{
						gesture.pickObject.transform.GetComponent<TPCCS>().highlight = true;
						players[1].GetComponent <TPCCS>().highlight = false;
					}
					else if (gesture.pickObject.transform == players[1])
					{
						gesture.pickObject.transform.GetComponent<TPCCS>().highlight = true;
						players[0].GetComponent <TPCCS>().highlight = false;
					}
				}
			}

There is no error however there is logical error. The correct logic is you must highlight the player first, before passing. First tap highlight, second tap pass.

What happens here is, P2 is highlighted on first tap with P1 as the owner of the ball. Then when P2 tapped the second time, P1 passes the ball to him. However, for P1, on first tap I tap on P1, it highlights and P2 straight away passes the ball to P1, without waiting for the second tap.

Can I know why P2 works but not P1? I really need help over here.
Thanks in advance.

Some general comments on glancing at the above:

Firstly, assign the results of all GetComponent calls (e.g. soccerBall.GetComponent().myOwner, gesture.pickObject.transform.GetComponent ()) to variables once at the Start() of your script and then reference them by variable. This will make your code both faster and easier to read.

Second, in this section, you’re setting highlight=true multiple times (once unconditionally and once in both branches of the if):

 gesture.pickObject.transform.GetComponent <TPCCS>().highlight = true;
if(gesture.pickObject.transform == players[0])
{
gesture.pickObject.transform.GetComponent<TPCCS>().highlight = true;
players[1].GetComponent <TPCCS>().highlight = false;
}
else if (gesture.pickObject.transform == players[1])
{
gesture.pickObject.transform.GetComponent<TPCCS>().highlight = true;
players[0].GetComponent <TPCCS>().highlight = false;
}

Finally, since the script is symmetric, I assume that when you drag the components to the players array you’ll need to reverse the order. i.e. On P1’s SctiptA, player[0] is P1 and player[1] is P2. On P2’s ScriptA, player[0] is P2 and player[1] is P1.