Search Unity

Send Rotations and Materials to Clients

Discussion in 'Multiplayer' started by wxxhrt, Oct 7, 2015.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Hi,
    Networking is new to me. I'm trying to send the scale and material name of non instantiated objects from client to server. The scale and material are randomly assigned on the server but its not properly getting across. Would be great if someone could see what I'm doing wrong here. My guess is that the Server script sends the material name and scale when it starts up but as no client is yet connected it's not there to receive anything?

    Thanks in advance


    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.Networking;
    3. import DG.Tweening;
    4.  
    5.  
    6. var originalVec : Vector3;
    7. var distanceVec : Vector3;
    8. var tween : Tween;
    9. var materialArr : Material[];
    10.  
    11.  
    12. class classTube extends NetworkBehaviour
    13. {
    14.     function Start()
    15.     {
    16.  
    17.         if (isServer)
    18.         {  
    19.             Debug.Log("onserver");
    20.             this.transform.rotation = Random.rotation;
    21.             this.transform.localScale = Vector3(Random.value * 5, Random.value * 5, Random.value * 5);
    22.            
    23.             originalVec = this.transform.position;
    24.             distanceVec = originalVec;
    25.             distanceVec.x += 10;
    26.            
    27.             this.gameObject.GetComponent.<Renderer>().sharedMaterial = materialArr[Random.Range(0,materialArr.Length)];
    28.            
    29.             //netcorrector is used to adjust object's scales and send through the chosen colour
    30.             var nc : ClassNetCorrector = this.gameObject.GetComponent.<ClassNetCorrector>();
    31.             nc.scale = this.transform.localScale;
    32.             nc.matStringName = this.gameObject.GetComponent.<Renderer>().sharedMaterial.name;
    33.         }
    34.     }
    35.    
    36.     function Update()
    37.     {
    38.         if (isServer)
    39.         {
    40.             if (Input.GetKeyDown("space"))
    41.             {
    42.                 Trigger(Random.value * 2);
    43.             }
    44.         }
    45.     }
    46.    
    47.     function Trigger(time : float)
    48.     {  
    49.         var randomVec : Vector3 = Vector3.Lerp(originalVec, distanceVec, Random.value);
    50.         tween.Kill(false);
    51.         //mySequence = DOTween.Sequence();
    52.         tween = DOMove(this.transform, randomVec, time, false);
    53.        
    54.     }
    55. }
    And the netcorrector script
    Code (JavaScript):
    1. import UnityEngine.Networking;
    2.  
    3. class ClassNetCorrector extends NetworkBehaviour {
    4.    
    5.    
    6.     @SyncVar
    7.     var scale : Vector3;
    8.    
    9.     @SyncVar
    10.     var matStringName : String;
    11.    
    12.  
    13.     function Start () {
    14.        
    15.         this.transform.localScale = scale;
    16.        
    17.         this.gameObject.GetComponent.<Renderer>().sharedMaterial = Resources.Load("Materials/" + matStringName) as Material;
    18.     }
    19. }
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    use OnStartClient() instead of Start()
     
    wxxhrt likes this.
  3. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Thanks!