Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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,026
    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,026
    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.