Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Updating SyncVar of SceneObject from clients

Discussion in 'Multiplayer' started by leoggwp, Aug 8, 2018.

  1. leoggwp

    leoggwp

    Joined:
    Feb 22, 2018
    Posts:
    10
    In my project, there are generator prefabs placed in the scene. The Generators got the NetworkIdentity with LocalPlayerAuthority.

    So now I want every Client to be able to Change a value in the GeneratorController-Script. More precisely, if a Client is next to the Generator(2D Collider detection), it gets loaded until 100%. A Slider(child of Generator) is getting value from GeneratorController-Script automatically for displaying the value.

    In my Code I am checking if the maxAmount is reached, so the bool filled is set to true. I just want to update the fillAmount for every Client, so they can set the filled bool by themselves on their game.

    I tested often(but don't exactly know what is the Problem here. I think i found out that if the Client, which have to be tagged as hider(checked in Trigger-function) has to be the Player who created the Game.

    Thanks for your help :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityEngine.UI;
    6.  
    7. public class GeneratorController : NetworkBehaviour {
    8.  
    9.     [SyncVar]
    10.     public float fillAmount;
    11.  
    12.     public float maxAmount;
    13.  
    14.    
    15.     public bool filled = false;
    16.     public float fillSpeed;
    17.  
    18.     public Slider slider;
    19.  
    20.     public void Start()
    21.     {
    22.         fillSpeed = 1;
    23.         maxAmount = 100;
    24.         SetUp();
    25.     }
    26.  
    27.     public void SetUp()
    28.     {
    29.         fillAmount = 0;
    30.         filled = false;
    31.     }
    32.  
    33.     public void Update()
    34.     {
    35.         if(fillAmount >= maxAmount)
    36.         {
    37.             filled = true;          
    38.         }
    39.     }
    40.  
    41.      public void OnTriggerStay2D(Collider2D collision)
    42.     {
    43.         if (collision.gameObject.tag == "Player")
    44.         {
    45.             PlayerController otherPlayerController = collision.gameObject.GetComponent<PlayerController>();
    46.             if (otherPlayerController.mainPlayer == true && otherPlayerController.hider == true)
    47.             {
    48.                 CmdAddFill(fillSpeed * Time.deltaTime);
    49.             }
    50.         }      
    51.  
    52.     }
    53.  
    54.        
    55.     [Command]
    56.     public void CmdAddFill(float amount)
    57.     {
    58.         fillAmount += amount;      
    59.     }
    60.        
    61. }
    62.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    SyncVars don't update from the client to the server. Commands won't send on an object that the client doesn't have authority, and you can't assign authority of 1 object to multiple clients at the same time.

    I would control this by sending a Unet Message from the client to the server. Unet Messages don't involve object authority. Alternatively you could send a Command to the server from the client's Player GameObject to control this other object.
     
  3. leoggwp

    leoggwp

    Joined:
    Feb 22, 2018
    Posts:
    10
    So now i wanted to try by letting the Player gameObject execute the command like this:
    Code (CSharp):
    1. public void OnTriggerStay2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Generator" && isLocalPlayer)
    4.         {
    5.             GeneratorController otherGeneratorController = collision.gameObject.GetComponent<GeneratorController>();
    6.             if (otherGeneratorController.filled == false)
    7.             {
    8.                 CmdAddFill(otherGeneratorController, fillSpeed * Time.deltaTime);
    9.             }
    10.         }
    11.     }
    12.  
    13.     [Command]
    14.     public void CmdAddFill(GeneratorController targetGen, float fillAdd)
    15.     {
    16.         targetGen.fillAmount += fillAdd;
    17.     }
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Commands cannot send custom classes.

    https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html
     
  5. leoggwp

    leoggwp

    Joined:
    Feb 22, 2018
    Posts:
    10
    Thank you very much. It works now by passing the GameObject.