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 static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
     {
         if (!connected || (InstantiateInRoomOnly && !inRoom))
         {
             Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
             return null;
         }

         GameObject prefabGo;
         if (!UsePrefabCache || !PrefabCache.TryGetValue(prefabName, out prefabGo))
         {
             prefabGo = (GameObject)Resources.Load(prefabName, typeof(GameObject));
             if (UsePrefabCache)
             {
                 PrefabCache.Add(prefabName, prefabGo);
             }
         }

         if (prefabGo == null)
         {
             Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
             return null;
         }

         // a scene object instantiated with network visibility has to contain a PhotonView
         if (prefabGo.GetComponent() == null)
         {
             Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
             return null;
         }

         Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
         int[] viewIDs = new int[views.Length];
         for (int i = 0; i < viewIDs.Length; i++)
         {
             //Debug.Log("Instantiate prefabName: " + prefabName + " player.ID: " + player.ID);
             viewIDs[i] = AllocateViewID(player.ID);
         }

         // Send to others, create info
         Hashtable instantiateEvent = networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false);

         // Instantiate the GO locally (but the same way as if it was done via event). This will also cache the instantiationId
         return networkingPeer.DoInstantiate(instantiateEvent, networkingPeer.mLocalActor, prefabGo);
     }