Search Unity

[SOLVED] (2D) Clients dont change new Instantiated Object values

Discussion in 'Multiplayer' started by Gunging, Jan 12, 2017.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    I have this special weapon, "Charge Cannon", while you hold spacebar it charges, and when you release it, if you are Local Player it calls CmdFire with the charged value, works in singleplayer, but in Multiplayer, only the host instantiates the prefab with the correct values.

    Code (CSharp):
    1.  
    2. [Command] void CmdFire(int chargedSize) {
    3.     GameObject newChargeCannonball = (GameObject)Instantiate(chargeCannonballPrefab, firePoint.transform.position, Quaternion.identity);            //Cannonball prefab is instantiated.
    4.     newChargeCannonball.transform.localScale = new Vector3(chargedSize* 0.005f * 0.15f, chargedSize* 0.005f * 0.15f, chargedSize* 0.005f * 0.15f);   //Cannonball prefab's size is changed to a size corresponding its charge.
    5.     newChargeCannonball.GetComponent<ChargeCannonScript>().chargeSize = chargedSize;        //Change the Cannonball's Script variable "chargeSize" to what the player sent CmdFire to run with.
    6.     newChargeCannonball.GetComponent<ChargeCannonScript>().transformUp = transform.up;      //Explained by itself
    7.     if (chargedSize >= 50) { newChargeCannonball.tag = "Explode"; }                          //If it is "big" enough, the tag Explode makes other game effects (like destroying stuff), so player cant use the destructive force of this if they dont charge it enough
    8.     Physics2D.IgnoreCollision(GetComponent<Collider2D>(), newChargeCannonball.gameObject.GetComponent<Collider2D>(), true); //So the player does not damages himself
    9.     NetworkServer.Spawn(newChargeCannonball); //Object is spawned with all the changes done?
    10. }
    11.  
    However, clients dont chage the value, usually, chargedSize is 50 (the max), cause in my tests I like to fully charge it, and I have noticed, the client's spawned ChargeCannonballs have always a charge of 0 (default), so they instantly destroy (cause the charge Cannonball's lifetime is 0.05 times the charge size, to a max of 2.5 seconds, so when charge is 0, lifetime is 0 and destroys (This is calculated inside the not relevant cause perfectly working ChargeCannonball's script)), Also, if default charge size is set to a greater number, it wont move, cause transformUp was never changed (Instead of transform.up, chargeCannonball moves in direction TransformUp), also the tag is always the Default, instead of Explode when fully charged, and the player still collides with it.
    (This only happens to clients, host works perfectly fine, however, host's projectiles are not seen in clients)

    I am truly confused, in many tutorials I have seen placing NetworkServer.Spawn() after changing values was the solution, but this problem exists. thanks in advance.

    EDIT: HOW TO SOVLE:
    1: For any values able to be Syned by [SyncVar], just add [SyncVar]. it solves it.
    Ej. public bool sampleBool; ----> [SyncVar]public bool sampleBool;

    2: For GameObjects, send instead their NetworkInstanceId which can be Synced by [SyncVar].
    then, the way it worked for me is the following:

    Code (CSharp):
    1. GameObject gameobjectYouWantToSync;
    2. [SyncVar] public NetworkInstanceId theId;
    3.  
    4. void Update(){
    5.       if (gameobjectYouWantToSync == null) {
    6.             gameobjectYouWantToSync = ClientScene.FindLocalObject(theId);
    7.       }
    8. //This is the spawned object's script.
    9. }
    *To send the NetworkInstanceId to the instantated object, use:
    Code (CSharp):
    1.  
    2.         GameObject obj = (GameObject)Instantiate(samplePrefab, transform.position, transform.rotation);
    3.         obj.GetComponent<SampleScript>().theId = GetComponent<NetworkIdentity>().netId;
    4.         NetworkServer.Spawn(obj);
    3: I dont needed to sync anything more, so I didnt made a solution for it, so I cant provde it.
     
    Last edited: Feb 26, 2017
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    You probably need to read up in the UNET docs about the concept of Authority and how that works with Spawning, and how that effects who can send what messages where.
     
  3. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139