Search Unity

How do I add a NetworkView to other classes?

Discussion in 'Scripting' started by Yura_steve191, Jun 14, 2018.

  1. Yura_steve191

    Yura_steve191

    Joined:
    May 15, 2017
    Posts:
    1
    How do I add a NetworkView to other classes?
    I have some problem with calling the [command] and [clientrpc] on any classes in my scripts.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class Sync : MonoBehaviour {
    7.     public bool position, rotation, child;
    8.     public bool syncChildTransform,syncChildRotation;
    9.     public GameObject[] childObjs;
    10.     Sync_NetManager sync = new Sync_NetManager();
    11.     NetworkIdentity id;
    12.     void Update(){
    13.         if (id == null)
    14.             id = transform.GetComponent<NetworkIdentity>();
    15.         if (!sync.isLocal(id))
    16.             return;
    17.         if(child)
    18.         sync.SetChildObjects(childObjs);
    19.        
    20.         SyncManager();
    21.     }
    22.     private void SyncManager(){
    23.         if (transform) {
    24.             if (child) {
    25.                
    26.             } else {
    27.                 sync.CmdSync(1,2,transform.position,transform.rotation);
    28.             }
    29.         }
    30.         if (rotation) {
    31.             if (child) {
    32.                
    33.             } else {
    34.                 sync.CmdSync(2,2,transform.position,transform.rotation);
    35.             }
    36.         }
    37.     }
    38. }
    39.  
    40.  
    41.  
    42. public class Sync_NetManager : NetworkBehaviour {
    43.     private GameObject[] _childObjs;
    44.  
    45.     public bool isLocal(NetworkIdentity id){
    46.         return id.isLocalPlayer;
    47.     }
    48.     public void SetChildObjects(GameObject[] childObjs){
    49.         this._childObjs = childObjs;
    50.     }
    51.  
    52.     [Command]
    53.     public void CmdSync(int type, int num, Vector3 pos, Quaternion rot){
    54.         RpcSync(type,num,pos,rot);
    55.     }
    56. }
    Error:
    NullReferenceException
    UnityEngine.Component.GetComponent[NetworkIdentity];
    How can I solve it?