Send Rotations and Materials to Clients

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

#pragma strict
import UnityEngine.Networking;
import DG.Tweening;


var originalVec : Vector3;
var distanceVec : Vector3;
var tween : Tween;
var materialArr : Material[];


class classTube extends NetworkBehaviour
{
    function Start()
    {

        if (isServer)
        {   
            Debug.Log("onserver");
            this.transform.rotation = Random.rotation;
            this.transform.localScale = Vector3(Random.value * 5, Random.value * 5, Random.value * 5);
           
            originalVec = this.transform.position;
            distanceVec = originalVec;
            distanceVec.x += 10;
           
            this.gameObject.GetComponent.<Renderer>().sharedMaterial = materialArr[Random.Range(0,materialArr.Length)];
            
            //netcorrector is used to adjust object's scales and send through the chosen colour
            var nc : ClassNetCorrector = this.gameObject.GetComponent.<ClassNetCorrector>();
            nc.scale = this.transform.localScale;
            nc.matStringName = this.gameObject.GetComponent.<Renderer>().sharedMaterial.name;
        }
    }
   
    function Update()
    {
        if (isServer)
        {
            if (Input.GetKeyDown("space"))
            {
                Trigger(Random.value * 2);
            }
        }
    }
   
    function Trigger(time : float)
    {   
        var randomVec : Vector3 = Vector3.Lerp(originalVec, distanceVec, Random.value);
        tween.Kill(false);
        //mySequence = DOTween.Sequence();
        tween = DOMove(this.transform, randomVec, time, false);
       
    }
}

And the netcorrector script

import UnityEngine.Networking;

class ClassNetCorrector extends NetworkBehaviour {
   
   
    @SyncVar
    var scale : Vector3;
   
    @SyncVar
    var matStringName : String;
   

    function Start () {
       
        this.transform.localScale = scale;
       
        this.gameObject.GetComponent.<Renderer>().sharedMaterial = Resources.Load("Materials/" + matStringName) as Material;
    }
}

use OnStartClient() instead of Start()

1 Like

Thanks!