Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer Updating SyncVar of SceneObject from clients

Discussion in 'Connected Games' started by leoggwp, Aug 8, 2018.

  1. leoggwp

    leoggwp

    Joined:
    Feb 22, 2018
    Posts:
    7
    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:
    3,607
    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:
    7
    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:
    3,607
    Commands cannot send custom classes.

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

    leoggwp

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