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:

         private void AddGameObjectsTo(GameObject parent, XElement xml, string objPath, IList customImporters)
         {
             foreach (XElement goXml in xml.Elements("GameObject"))
             {
                 string name = ImportUtils.GetAttributeAsString(goXml, "name", "");
                 string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", "");

                 GameObject child = null;
                 if (!String.IsNullOrEmpty(copyFrom))
                 {
                     child = CreateCopyFromMeshObj(copyFrom, objPath);
                     if (child == null)
                     {
                         // We're in trouble. Errors should already be in the log.
                         return;
                     }
                 }
                 else
                 {
                     child = new GameObject();
                 }

                 if (!String.IsNullOrEmpty(name))
                 {
                     child.name = name;
                 }

                 float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0);
                 float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0);
                 child.transform.position = new Vector3(x, y, 0);

                 // Assign the child to the parent
                 child.transform.parent = parent.transform;

                 // Do we have any collision data
                 AddCollidersTo(child, goXml);

                 // Do we have any children of our own?
                 AddGameObjectsTo(child, goXml, objPath, customImporters);

                 // Does this game object have a tag?
                 AssignTagTo(child, goXml);

                 // Does this game object have a layer?
                 AssignLayerTo(child, goXml);

                 // Are there any custom properties?
                 HandleCustomProperties(child, goXml, customImporters);
             }
         }