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

         if (!isMasterClient)
         {
             Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Client is not the MasterClient in this room.");
             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 InstantiateSceneObject 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 InstantiateSceneObject prefab:" + prefabName + ". Prefab must have a PhotonView component.");
             return null;
         }

         Component[] views = (Component[])prefabGo.GetPhotonViewsInChildren();
         int[] viewIDs = AllocateSceneViewIDs(views.Length);

         if (viewIDs == null)
         {
             Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". No ViewIDs are free to use. Max is: " + MAX_VIEW_IDS);
             return null;
         }

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

         // 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);
     }