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:

   protected virtual void AttemptMove (int xDir, int yDir)
   {
    //Hit will store whatever our linecast hits when Move is called.
    RaycastHit2D hit;

    //Set canMove to true if Move was successful, false if failed.
    bool canMove = Move (xDir, yDir, out hit);

    //Check if nothing was hit by linecast
    if(hit.transform == null)
     //If nothing was hit, return and don't execute further code.
     return;

    //Get a component reference to the component of type T attached to the object that was hit
    T hitComponent = hit.transform.GetComponent ();

    //If canMove is false and hitComponent is not equal to null, meaning MovingObject is blocked and has hit something it can interact with.
    if(!canMove && hitComponent != null)

     //Call the OnCantMove function and pass it hitComponent as a parameter.
     OnCantMove (hitComponent);
   }