Script for soccer score - Team with more goals wins Help

Hello,

In a soccer project i want to develop a code that determines the scores, for example at the end of the game the team with 1-0 wins

Maybe something like:

 If (value of team 1 > score team 2) 
{ 
Win_Menu.setactive (true); 
} 

If (score team 1 = score team 2) 
{ 
Draw_Menu.setactive (true); 
} 

If (score team 1 < score team 2) 
{
 Lose_Menu.setactive (true); 
}

And i will also have to create other line of the code to attach in a button that when is scored a goal, we press the button and it puts +1 the score
for example 1-0

Like for example:

Public void GoalTeam1 ()
{
Score Team 1 = +1;
}

Please i need help

Thanks,

PMelodeveloper

You’ve got the logic pretty well down pat :slight_smile:

Just change ‘Score team 1’ and ‘score team 2’ to an integer and give it a legal variable name. For example:

public GameObject win_menu;
public GameObject lose_menu;
public GameObject draw_menu;

int team1Score;
int team2Score;

public void ChooseWinner()
{
      If (value of team 1 > score team 2) 
     { 
         win_Menu.setactive (true); 
     } 
 
     if (score team 1 == score team 2) 
     { 
     Draw_Menu.setactive (true); 
     } 
 
     if (score team 1 < score team 2) 
     {
         Lose_Menu.setactive (true); 
     }
}

public void ResetScores(){
    team1Score = 0;
    team2Score = 0;
}

public void TeamOneGoal(){
    team1Score++;
}

public void TeamTwoGoal(){
    team2Score++;
}

Something like this would be most extensible and easier to read I would think than your current implementation.

Just add them to a list and sort them highest to lowest by the score and then take the first item.
Note: that most of the code here is literally test code.

It allows for you to use it as the basis for games where there are more than 2 teams and it specifies a standard interface for the teams and the games i.e. you have a game, it has a collection of teams of those teams there will be a single winner.

It is written using Linq for clarity but the principal can be applied without incurring the additional allocations.


      public class Team 
        {
            public int Score { get; set; }
        }

        public class Game
        {
            public List<Team> Teams { get; set; } = new List<Team>();
            public Team WinningTeam => Teams.OrderByDescending(t => t.Score).FirstOrDefault();
        }

        public class TestClass
        {
            public void Test ()
            {
                var TeamA = new Team { Score = 12 };
                var TeamB = new Team { Score = 4 };

                var Game = new Game();
                Game.Teams.Add(TeamA);
                Game.Teams.Add(TeamB);

                var winningTeam = Game.WinningTeam;
            }
        }