Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I need help with an error please

Discussion in 'Scripting' started by Razzy1199, Jan 28, 2020.

  1. Razzy1199

    Razzy1199

    Joined:
    Jan 25, 2020
    Posts:
    6
    I have an error
    Assets/Scripts/Game_controler.cs(84,10): error CS0161: 'Game_controler.WinnerCheck()': not all code paths return a value
    there is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Game_controler : MonoBehaviour {
    7.  
    8.     public int whoseTurn; //0 = x and 1 = o
    9.     public int turnCount; // counts the number of turns played
    10.     public GameObject[] turnIcons; // displays whos turn it is
    11.     public Sprite[] playIcons; //0 = x icon and 1 = o icon
    12.     public Button[] tictactoeSpaces;//playable spaces for our game
    13.     public int[] markedSpaces;//Id's which spaces was marked by which player
    14.     public Text winnerText;//Holds the text component of the winner text
    15.     public GameObject[] winningLine;// hold all the differtent lines to show that theres a winner
    16.     public GameObject winnerPanel;
    17.     public int xPlayersScore;
    18.     public int oPlayersScore;
    19.     public Text xPlayersScoreText;
    20.     public Text oPlayersScoreText;
    21.     public Button xPlayersButton;
    22.     public Button oPlayersButton;
    23.     public GameObject drawImage;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         GameSetup();
    29.     }
    30.     void GameSetup()
    31.     {
    32.         whoseTurn = 0;
    33.         turnCount = 0;
    34.         turnIcons[0].SetActive(true);
    35.         turnIcons[1].SetActive(false);
    36.         for(int i = 0; i < tictactoeSpaces.Length; i++)
    37.         {
    38.             tictactoeSpaces[i].interactable = true;
    39.             tictactoeSpaces[i].GetComponent<Image>().sprite = null;
    40.         }
    41.         for(int i = 0; i < markedSpaces.Length; i++)
    42.         {
    43.             markedSpaces[i] = -100;
    44.         }
    45.     }
    46.  
    47.  
    48.     // Update is called once per frame
    49.     void Update()
    50.     { }
    51.         public void TicTacToeButton(int WhichNumber)
    52.     {
    53.         xPlayersButton.interactable = false;
    54.         oPlayersButton.interactable = false;
    55.         tictactoeSpaces[WhichNumber].image.sprite = playIcons[whoseTurn];
    56.         tictactoeSpaces[WhichNumber].interactable = false;
    57.  
    58.  
    59.         markedSpaces[WhichNumber] = whoseTurn+1;
    60.         turnCount++;
    61.         if(turnCount > 4)
    62.         {
    63.             bool isWinner = WinnerCheck();
    64.             if(turnCount == 9 && isWinner == false)
    65.             {
    66.              Draw();
    67.             }
    68.         }
    69.  
    70.         if (whoseTurn == 0)
    71.         {
    72.             whoseTurn = 1;
    73.             turnIcons[0].SetActive(false);
    74.             turnIcons[1].SetActive(true);
    75.         }
    76.         else
    77.         {
    78.             whoseTurn = 0;
    79.             turnIcons[0].SetActive(true);
    80.             turnIcons[1].SetActive(false);
    81.         }
    82.  
    83.     }
    84.     bool WinnerCheck()
    85.     {
    86.         int s1 = markedSpaces[0] + markedSpaces[1] + markedSpaces[2];
    87.         int s2 = markedSpaces[3] + markedSpaces[4] + markedSpaces[5];
    88.         int s3 = markedSpaces[6] + markedSpaces[7] + markedSpaces[8];
    89.         int s4 = markedSpaces[0] + markedSpaces[3] + markedSpaces[6];
    90.         int s5 = markedSpaces[1] + markedSpaces[4] + markedSpaces[7];
    91.         int s6 = markedSpaces[2] + markedSpaces[5] + markedSpaces[8];
    92.         int s7 = markedSpaces[0] + markedSpaces[4] + markedSpaces[8];
    93.         int s8 = markedSpaces[0] + markedSpaces[4] + markedSpaces[6];
    94.         var solutions = new int[] { s1, s2, s3, s4, s5, s6, s7, s8 };
    95.         for (int i = 0; i < solutions.Length; i++)
    96.         {
    97.             if(solutions[i] == 3*(whoseTurn+1))
    98.             {
    99.                 WinnerDisplay(i);
    100.                 return true;
    101.             }
    102.             return false;
    103.         }
    104.         void WinnerDisplay(int indexIn)
    105.         {
    106.             winnerPanel.gameObject.SetActive(true);
    107.             if (whoseTurn == 0)
    108.             {
    109.                 xPlayersScore++;
    110.                 xPlayersScoreText.text = xPlayersScore.ToString();
    111.                 winnerText.text = "Player X Won!";
    112.             }
    113.             else if(whoseTurn == 1)
    114.             {
    115.                 oPlayersScore++;
    116.                 oPlayersScoreText.text = oPlayersScore.ToString();
    117.                 winnerText.text = "Player O Won!";
    118.             }
    119.             winningLine[indexIn].SetActive(true);
    120.          
    121.                  
    122.         }
    123.     }
    124.     public void Rematch()
    125.     {
    126.         GameSetup();
    127.         for(int i = 0; i < winningLine.Length; i++)
    128.         {
    129.             winningLine[i].SetActive(false);
    130.         }
    131.         winnerPanel.SetActive(false);
    132.         xPlayersButton.interactable = true;
    133.         oPlayersButton.interactable = true;
    134.         drawImage.SetActive(false);
    135.     }
    136.     public void Restart()
    137.     {
    138.         Rematch();
    139.         xPlayersScore = 0;
    140.         oPlayersScore = 0;
    141.         xPlayersScoreText.text = "0";
    142.         oPlayersScoreText.text = "0";
    143.     }
    144.     public void SwitchPlayer(int whichPlayer)
    145.     {
    146.         if(whichPlayer == 0)
    147.         {
    148.             whoseTurn = 0;
    149.             turnIcons[0].SetActive(true);
    150.             turnIcons[1].SetActive(false);
    151.         }
    152.         else if(whichPlayer == 1)
    153.         {
    154.             whoseTurn = 1;
    155.             turnIcons[0].SetActive(false);
    156.             turnIcons[1].SetActive(true);
    157.         }
    158.      
    159.     }
    160.     void Draw()
    161.     {
    162.         winnerPanel.SetActive(true);
    163.         drawImage.SetActive(true);
    164.         winnerText.text = "Draw";
    165.     }
    166. }
    please help me !!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    One issue is that you have one function inside another. Pay attention to your curly brackets: WinnerDisplay is inside WinnerCheck. You need another } before WinnerDisplay and one less at the end of that function.

    And, your "return false", I think, should probably be outside the for loop, which would resolve the actual error message.
     
  3. Razzy1199

    Razzy1199

    Joined:
    Jan 25, 2020
    Posts:
    6
    Thank you very much! Your advice was very helpful. :)