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 internal void RemoveInstantiatedGO(GameObject go, bool localOnly)
     {
         if (go == null)
         {
             Debug.LogError("Failed to 'network-remove' GameObject because it's null.");
             return;
         }

         // Don't remove the GO if it doesn't have any PhotonView
         PhotonView[] views = go.GetComponentsInChildren(true);
         if (views == null || views.Length <= 0)
         {
             Debug.LogError("Failed to 'network-remove' GameObject because has no PhotonView components: " + go);
             return;
         }

         PhotonView viewZero = views[0];
         int creatorId = viewZero.CreatorActorNr; // creatorId of obj is needed to delete EvInstantiate (only if it's from that user)
         int instantiationId = viewZero.instantiationId; // actual, live InstantiationIds start with 1 and go up

         // Don't remove GOs that are owned by others (unless this is the master and the remote player left)
         if (!localOnly)
         {
             if (!viewZero.isMine)
             {
                 Debug.LogError("Failed to 'network-remove' GameObject. Client is neither owner nor masterClient taking over for owner who left: " + viewZero);
                 return;
             }

             // Don't remove the Instantiation from the server, if it doesn't have a proper ID
             if (instantiationId < 1)
             {
                 Debug.LogError("Failed to 'network-remove' GameObject because it is missing a valid InstantiationId on view: " + viewZero + ". Not Destroying GameObject or PhotonViews!");
                 return;
             }
         }


         // cleanup instantiation (event and local list)
         if (!localOnly)
         {
             this.ServerCleanInstantiateAndDestroy(instantiationId, creatorId, viewZero.isRuntimeInstantiated); // server cleaning
         }


         // cleanup PhotonViews and their RPCs events (if not localOnly)
         for (int j = views.Length - 1; j >= 0; j--)
         {
             PhotonView view = views[j];
             if (view == null)
             {
                 continue;
             }

             // we only destroy/clean PhotonViews that were created by PhotonNetwork.Instantiate (and those have an instantiationId!)
             if (view.instantiationId >= 1)
             {
                 this.LocalCleanPhotonView(view);
             }
             if (!localOnly)
             {
                 this.OpCleanRpcBuffer(view);
             }
         }

         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
             Debug.Log("Network destroy Instantiated GO: " + go.name);

         GameObject.Destroy(go);
     }