Search Unity

Adding script to object cause Networkidentity to disapear

Discussion in 'Multiplayer' started by pKallv, Mar 5, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have a script were i create child objects to a player (Pointer) to move etc. All works fine between the clients.

    I then add an empty script to the GameObject (Orange) and suddenly i get the following message:

    This script will be developed into a rotation sync function.

    Again, all works well until i add this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class TestScript : NetworkBehaviour {
    6.  
    7. }
    Initially had code in the script but when getting the error i started to test and removed all code so it is empty.

    Here is the relevant code:

    Code (CSharp):
    1. public void On_Drag(Gesture gesture) {
    2.         if (!isLocalPlayer)
    3.             return;
    4.  
    5.  
    6.         if (singleTouch) {
    7.  
    8.             if (firstRun) {
    9.  
    10.                 firstRun = false;
    11.  
    12.                 linecast_GameObject_HashSet.Clear ();
    13.                 linecast_GameObject_HashSet = DoLinecast ();
    14.                 linecast_GameObject_List = new List<GameObject> (linecast_GameObject_HashSet);
    15.  
    16.                 if (linecast_GameObject_List.Count > 0) {
    17.                     selected_GameObject = linecast_GameObject_List [0];
    18.                     if (selected_GameObject == null)
    19.                         print ("NULL");
    20.  
    21.                     childGameObject = GameObject.FindWithTag (linecast_GameObject_List [0].tag);
    22.                     childGameObject.transform.parent = myTransform.transform;
    23.                     print ("childGameObject: " + childGameObject.tag);
    24.                     // Assign Network Authority
    25.                     NetworkIdentity myNetID = childGameObject.GetComponent<NetworkIdentity> ();
    26. >>ERROR   print ("GO2: " + childGameObject.tag + " // myNetID: " + myNetID.netId);
    27.  
    28. //                    print ("isLocalPlayer: " + myNetID2.isLocalPlayer + " " + myNetID2.isServer);
    29.                     CmdAssignNetworkAuthority (myNetID.netId);
    30.  
    31.  
    32.                 }
    33.             }
    34.  
    35.         }
    36.  
    37.         gesture.pickedObject.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (gesture.position.x, gesture.position.y, 1f));
    38.  
    39.     }
    The line that is causing the problem is:
    Code (CSharp):
    1. print ("GO2: " + childGameObject.tag + myNetID: " + myNetID.netId);
    When is change the code line to this:
    Code (CSharp):
    1. print ("GO2: " + childGameObject.tag);
    ...removing the netID it works. This mean that somewere down the line the object losing it's Networkidentity when adding the TestScript? ...again, it works well without the TestScript. I need to add a script to synchronize rotation.

    I do the parent/children connection in this function:
    Code (CSharp):
    1. HashSet<GameObject> DoLinecast () {
    2.         GameObject linecast_GameObject;
    3.         HashSet<GameObject> rayHashSet = new HashSet<GameObject> ();
    4.  
    5.         LayerMask theLayer;
    6.         theLayer = (1 << LayerMask.NameToLayer("Fruit")); //set the layer to be clickable
    7.         Vector2 clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.  
    9.         //Get current worldspace position of mouse cursor
    10.         RaycastHit2D[] hits = Physics2D.LinecastAll(clickedPos,clickedPos,theLayer);
    11.  
    12.         foreach (RaycastHit2D ray in hits) {
    13.             linecast_GameObject = GameObject.Find(ray.collider.name);
    14.             rayHashSet.Add (linecast_GameObject);
    15.         }
    16.  
    17.         return rayHashSet;
    18.     }
    Again, when i remove the TestScript it works fine. Also, i use EasyTouch which also works fine.
     
    Last edited: Mar 6, 2016
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Tested this based on a suggestion:

    Code (CSharp):
    1. if(myNetID==null){
    2.     Debug.Log("myNetID is null");
    3. }
    4.  
    5. if(myNetID.netId==null){
    6.     Debug.Log("myNetID.netId is null");
    7. }
    and the first one is null.

    I still do not understand why the TestScript impact the init the way it does.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I tested to move the script up in the hierarchy, above the the NetworkIdentity and Networktransform and the error disappeared. I then tested with another GameObject and got the same result, error when the script was in the bottom, which disappeared when i moved it up in the hierarchy in the inspector.

    I am confused why this is happen, anyone that can explain?