Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

  public void SpawnPiece(GridCoords coords, GameObject piece, float yRotation, PlayerType playerType) {
   Node pieceNode = GetNodeAt(coords.row, coords.col);

   Vector3 pRotation = piece.transform.rotation.eulerAngles;
   Quaternion newPRotation = Quaternion.Euler(pRotation.x, yRotation, pRotation.z);

   GameObject pieceObject = Instantiate(piece, pieceNode.transform.position + Vector3.up * 1.2f, newPRotation) as GameObject;
   pieceObject.transform.localScale = Vector3.zero; //for scaling in start from zero
   Piece pieceScript = pieceObject.GetComponent(typeof(Piece)) as Piece;

   //assign mat and player type
   Material mat = null;
   GCPlayer player = null;
   switch (playerType) {
    case PlayerType.P1:
     mat = GameManager.Instance.PieceP1;
     player = GameManager.Instance.P1;
     break;
    case PlayerType.P2:
     mat = GameManager.Instance.PieceP2;
     player = GameManager.Instance.P2;
     break;
   }
   pieceObject.GetComponent().material = mat;
   player.AddPieces(pieceScript);
   pieceScript.PieceMovement = Creator.CreatePieceMovement(pieceScript.MovementType, player, pieceScript);

   if(pieceScript) //if exists type then scale
    pieceScript.ScaleIn(Random.Range(0f,1f),Random.Range(1f,2f),piece.transform.localScale);


   pieceScript.UpdateNode(pieceNode);
  }