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 setBlockNumber(int blockNumber) {
   this.blockNumber = blockNumber;
   TextMesh textMesh = this.GetComponentInChildren();
   Material blockTextMaterial = gameObject.transform.Find ("BlockText").gameObject.GetComponent().material;
   MeshRenderer cube = gameObject.GetComponentInChildren();
   Color cubeColor = cube.GetComponent().material.color;

   //block doesn't exits
   if(blockNumber == -2) {
    cube.GetComponent().enabled = false;
    textMesh.text = "";
   }
   //block is empty
   else if (blockNumber == -1 ) {
    cube.GetComponent().enabled = false;
    cube.GetComponent().material.color = new Color(1, 1, 1, 0.2f);
    textMesh.text = "";
   }
   //block has a value
   else {
    cube.GetComponent().enabled = true;
    cube.material.color = this.getColor (blockNumber);
    blockTextMaterial.SetColor ("_Color", new Color(1,1,1));
    if(blockNumber == 0) blockTextMaterial.SetColor ("_Color", new Color(0.8f,0.8f,1));
    textMesh.text = blockNumber.ToString();
    transform.position = this.originalPosition;
   }

  }