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

Error Script Please Help!

Discussion in 'Scripting' started by Arewzo, Apr 11, 2014.

  1. Arewzo

    Arewzo

    Joined:
    Jan 8, 2014
    Posts:
    16
    i don't know to fix this script :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GameManager : MonoBehaviour {
    6.  
    7.     public GameObject CubeDark;
    8.     public GameObject CubeLight;
    9.     public GameObject[] PiecesGO = new GameObject[6];
    10.     public int gameState = 0;           // In this state, the code is waiting for : 0 = Piece selection, 1 = Piece animation, 2 = Player2/AI movement
    11.     //private int activePlayer = 0;     // 0 = Player1, 1 = Player2, 2 = AI, to be used later
    12.     private GameObject SelectedPiece;   // Selected Piece
    13.     private int _boardHeight = -1;
    14.     private int _pieceHeight =  0;
    15.     private static int _boardSize = 7;
    16.     private int[,] _boardPieces = new int[_boardSize,_boardSize];
    17.  
    18.     // Initialize the board area
    19.     void Start()
    20.     {
    21.         CreateBoard();
    22.         AddPieces();
    23.     }
    24.    
    25.     // Create the board by placing cubes
    26.     void CreateBoard()
    27.     {
    28.        
    29.        
    30.         for(int i = 0; i < _boardSize; i++)
    31.         {
    32.             for(int j = 0; j < _boardSize; j++)
    33.             {
    34.                 if((i+j)%2 == 0)
    35.                 {
    36.                     Object.Instantiate(CubeDark,new Vector3(i,_boardHeight,j), Quaternion.identity);   
    37.                 }
    38.                 else
    39.                 {
    40.                     Object.Instantiate(CubeLight,new Vector3(i,_boardHeight,j), Quaternion.identity);
    41.                 }
    42.             }
    43.         }
    44.     }
    45.  
    46.     // Add all pieces are their respective position
    47.     void AddPieces()
    48.     {
    49.         int _linePosY;
    50.         int _piecePlayer;
    51.        
    52.         // Create all pawn at once
    53.         for(int i = 0; i < _boardSize; i++)
    54.        
    55.         // Create Down pieces
    56.         _linePosY = 0;
    57.         _piecePlayer = 1;
    58.         CreatePiece("Pawn"  , 0, _linePosY, _piecePlayer);
    59.         CreatePiece("Pawn", 1, _linePosY, _piecePlayer);
    60.         CreatePiece("Pawn", 5, _linePosY, _piecePlayer);
    61.         CreatePiece("Pawn" , 6, _linePosY, _piecePlayer);
    62.        
    63.         // Create Up pieces
    64.         _linePosY = 6;
    65.         _piecePlayer = -1;
    66.         CreatePiece("Pawn"  , 0, _linePosY, _piecePlayer);
    67.         CreatePiece("Pawn", 1, _linePosY, _piecePlayer);
    68.         CreatePiece("Pawn", 5, _linePosY, _piecePlayer);
    69.         CreatePiece("Pawn" , 5, _linePosY, _piecePlayer);
    70.  
    71.         // Creat Second Line Pieces
    72.         _linePosY = 1;
    73.         _piecePlayer = 1;
    74.         CreatePiece("Pawn"  , 0, _linePosY, _piecePlayer);
    75.         CreatePiece("Pawn", 6, _linePosY, _piecePlayer);
    76.  
    77.         // Creat Second Line Pieces
    78.         _linePosY = 5;
    79.         _piecePlayer = 1;
    80.         CreatePiece("Pawn"  , 0, _linePosY, _piecePlayer);
    81.         CreatePiece("Pawn", 6, _linePosY, _piecePlayer);
    82.     }
    83.    
    84.     // Spawn a piece on the board
    85.     void CreatePiece(string _pieceName, int _posX, int _posY, int _playerTag)
    86.     {
    87.                 GameObject _PieceToCreate = null;
    88.                 int _pieceIndex = 0;
    89.                 //Select the right prefab to instantiate
    90.                 switch (_pieceName)
    91.         {
    92.                 case "Pawn":
    93.                         _pieceIndex = 1;
    94.                         break;
    95.                 }
    96.                 _PieceToCreate = PiecesGO [_pieceIndex - 1];
    97.                 // Instantiate the piece as a GameObject to be able to modify it after
    98.                 _PieceToCreate = Object.Instantiate (_PieceToCreate, new Vector3 (_posX, _pieceHeight, _posY), Quaternion.identity) as GameObject;
    99.                 _PieceToCreate.name = _pieceName;
    100.         }
    101.     //Update SlectedPiece with the GameObject inputted to this function
    102.     public void SelectPiece(GameObject _PieceToSelect)
    103.     {
    104.         // Unselect the piece if it was already selected
    105.         if(_PieceToSelect  == SelectedPiece)
    106.         {
    107.             SelectedPiece.renderer.material.color = Color.white;
    108.             SelectedPiece = null;
    109.             ChangeState (0);
    110.         }
    111.         else
    112.         {
    113.             // Change color of the selected piece to make it apparent. Put it back to white when the piece is unselected
    114.             if(SelectedPiece)
    115.             {
    116.                 SelectedPiece.renderer.material.color = Color.white;
    117.             }
    118.             SelectedPiece = _PieceToSelect;
    119.             SelectedPiece.renderer.material.color = Color.blue;
    120.             ChangeState (1);
    121.         }
    122.     }
    123.    
    124.     // Move the SelectedPiece to the inputted coords
    125.     public void MovePiece(Vector2 _coordToMove)
    126.     {
    127.         bool validMovementBool = false;
    128.         Vector2 _coordPiece = new Vector2(SelectedPiece.transform.position.x, SelectedPiece.transform.position.z);
    129.        
    130.         // Don't move if the user clicked on its own cube or if there is a piece on the cube
    131.         if((_coordToMove.x != _coordPiece.x || _coordToMove.y != _coordPiece.y) || _boardPieces[(int)_coordToMove.x,(int)_coordToMove.y] != 0)
    132.         {
    133.             validMovementBool   = TestMovement (SelectedPiece, _coordToMove);
    134.         }
    135.        
    136.         if(validMovementBool)
    137.         {
    138.             _boardPieces[(int)_coordToMove.x, (int)_coordToMove.y] = _boardPieces[(int)_coordPiece.x, (int)_coordPiece.y];
    139.             _boardPieces[(int)_coordPiece.x , (int)_coordPiece.y ] = 0;
    140.            
    141.             SelectedPiece.transform.position = new Vector3(_coordToMove.x, _pieceHeight, _coordToMove.y);       // Move the piece
    142.             SelectedPiece.renderer.material.color = Color.white;    // Change it's color back
    143.             SelectedPiece = null;                                   // Unselect the Piece
    144.         }
    145.     }
    146.  
    147.     // Test if the piece can do the player's movement
    148.     bool TestMovement(GameObject _SelectedPiece, Vector2 _coordToMove)
    149.     {
    150.         bool _movementLegalBool = false;
    151.         bool _collisionDetectBool = false;
    152.         Vector2 _coordPiece = new Vector2(_SelectedPiece.transform.localPosition.x, _SelectedPiece.transform.localPosition.z);
    153.        
    154.         int _deltaX = (int)(_coordToMove.x - _coordPiece.x);
    155.         int _deltaY = (int)(_coordToMove.y - _coordPiece.y);
    156.         int activePlayerPawnPostion = 1;
    157.         //Debug.Log("Piece (" + _coordPiece.x + "," + _coordPiece.x + ") - Move (" + _coordToMove.x + "," + _coordToMove.y + ")");
    158.         //Debug.Log("Delta (" + _deltaX + "," + _deltaY + ")");
    159.         // Use the name of the _SelectedPiece GameObject to find the piece used
    160.         switch (_SelectedPiece.name)
    161.         {
    162.  
    163.         case "Pawn":
    164.             // Rook can move horizontally or vertically
    165.             if((_deltaX != 0  _deltaY == 0) || (_deltaX == 0  _deltaY != 0))
    166.             {
    167.                 _movementLegalBool = true;
    168.             }
    169.             break;
    170.            
    171.         default:
    172.             _movementLegalBool = false;
    173.             break;
    174.         }
    175.        
    176.         // If the movement is legal, detect collision with piece in the way. Don't do it with knight since they can pass over pieces.
    177.         if(_movementLegalBool  SelectedPiece.name != "Knight")
    178.         {
    179.             _collisionDetectBool = TestCollision (_coordPiece, _coordToMove);
    180.         }
    181.        
    182.         return (_movementLegalBool  !_collisionDetectBool);
    183.     }
    184.  
    185.     // Test if a unit is in the path of the tested movement
    186.     bool TestCollision(Vector2 _coordInitial,Vector2 _coordFinal)
    187.     {
    188.         bool CollisionBool = false;
    189.         int _deltaX = (int)(_coordFinal.x - _coordInitial.x);
    190.         int _deltaY = (int)(_coordFinal.y - _coordInitial.y);
    191.         int _incX = 0; // Direction of the incrementation in X
    192.         int _incY = 0; // Direction of the incrementation in Y
    193.         int i;
    194.         int j;
    195.        
    196.         // Calculate the increment if _deltaX/Y is different from 0 to avoid division by 0
    197.         if(_deltaX != 0)
    198.         {
    199.             _incX = (_deltaX/Mathf.Abs(_deltaX));
    200.         }
    201.         if(_deltaY != 0)
    202.         {
    203.             _incY = (_deltaY/Mathf.Abs(_deltaY));
    204.         }
    205.        
    206.         i = (int)_coordInitial.x + _incX;
    207.         j = (int)_coordInitial.y + _incY;
    208.        
    209.         while(new Vector2(i, j) != _coordFinal)
    210.         {
    211.            
    212.             if(_boardPieces[i,j] != 0)
    213.             {
    214.                 CollisionBool = true;
    215.                 break;
    216.             }
    217.            
    218.             i += _incX;
    219.             j += _incY;
    220.         }
    221.         Debug.Log (CollisionBool);
    222.         return CollisionBool;
    223.     }
    224.     // Change the state of the game
    225.     public void ChangeState(int _newState)
    226.     {
    227.         gameState = _newState;
    228.         Debug.Log ("GameState = " + _newState);
    229.     }
    230. }
    231.  
    i only edit it not make it.. but i get it not working, the error is :
    Assets/Script/GameManager.cs(57,42): error CS0165: Use of unassigned local variable `_linePosY'
    i don't know how to fix it

    thanks before
     
    Last edited: Apr 11, 2014
  2. Ermarrero

    Ermarrero

    Joined:
    Sep 18, 2012
    Posts:
    9
    This code is a mess, makes me want to redo it all
     
  3. Arewzo

    Arewzo

    Joined:
    Jan 8, 2014
    Posts:
    16
    You know the wrong lines?
     
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Line 52 makes me sad...

     
  5. Arewzo

    Arewzo

    Joined:
    Jan 8, 2014
    Posts:
    16
    why?
     
  6. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Because kittens all over the world are shedding tears looking at that line. All the loop does is to set _linePosY to zero exactly _boardSize times. Comment it out and the code will compile just fine. But I assume you originally intended to do something with the control variable i, didn't you?
     
    Last edited: Apr 11, 2014
  7. Arewzo

    Arewzo

    Joined:
    Jan 8, 2014
    Posts:
    16
    i don't understand what is the meaning do something with the control variable i, because i'm new :D
    now i can run it.. but i have another problem:(