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 Start()
         {
             string[] names = new string[] { "dog", "monkey", "pig", "fox", "giraffe", "panda", "rhino", "tiger", "elephant", "lion" };

             animals = new List();
             int animalIndex = 0;

             UpgradeInfo upgradeInfo = new UpgradeInfo();

             GameObject animalPlayer = (GameObject)Instantiate(Resources.Load("Animals/" + names[Attr.currentAnimal]));
             animalPlayer.transform.parent = gameObject.transform;
             animalPlayer.transform.localPosition = new Vector3(-3.5f + 0.7f * animalIndex, -0.9f, 0);
             setSortingLayer(animalPlayer, "Animal8");
             Animal player = animalPlayer.AddComponent();
             player.setAnimalName(names[Attr.currentAnimal]);
             player.animalIndex = animalIndex;
             player.gameScreen = gameScreen;
             float speedPlayer = upgradeInfo.getItem(Attr.currentAnimal, 0, false);
             float jumpPlayer = upgradeInfo.getItem(Attr.currentAnimal, 1, false);
             player.setAnimalProperties(speedPlayer, jumpPlayer);
             animalPlayer.layer = LayerMask.NameToLayer("Animal1");

             animals.Add(animalPlayer);
             animalIndex++;

             animalPlayer.GetComponent().Play("run");
             //animalPlayer.GetComponent().SetBool(Animal.IS_PLAYER, true);

             TextAsset xml = Resources.Load("Levels/WorldMap" + (Attr.currentWorld + 1));
             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.Load(new StringReader(xml.text));

             XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
             XmlNodeList levelNodeList = xmlNodeList.Item(Attr.currentLevel).ChildNodes;
             for (int i = 0; i < levelNodeList.Count; i++)
             {
                 int numberAnimal = int.Parse(levelNodeList.Item(i).Attributes.Item(0).Value);
                 string animalName = levelNodeList.Item(i).Attributes.Item(1).Value.ToLower();

                 for (int k = 0; k < numberAnimal; k++)
                 {
                     GameObject animalObject = (GameObject)Instantiate(Resources.Load("Animals/" + animalName));
                     animalObject.transform.parent = gameObject.transform;
                     animalObject.transform.localPosition = new Vector3(-3.5f + 0.7f*animalIndex, -0.9f, 0);
                     setSortingLayer(animalObject, "Animal" + animalIndex);
                     Animal animal = animalObject.AddComponent();
                     animal.setAnimalName(animalName.ToLower() + "_blue");
                     animal.animalIndex = animalIndex;
                     animal.gameScreen = gameScreen;
                     float speed = upgradeInfo.getSpeed(i);
                     float jump = upgradeInfo.getJump(i);
                     animal.setAnimalProperties(speed, jump);
                     animalObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
                     animals.Add(animalObject);
                     animalIndex++;

                     animalObject.GetComponent().Play("run_blue");

                     //animalObject.GetComponent().SetBool(Animal.IS_PLAYER, false);
                 }
             }
         }