Search Unity

Unity Network - How to send a float value from cliento to server

Discussion in 'Meta-forum Discussion' started by NocSchecter, Jun 20, 2018.

  1. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    hello, I have a problem sending values from a client to a server in unity network.
    I have a class that sends the percentage of the battery in the client and that will be updating in the server but when I run client-server, the server does not receive data and the client shows me that:

    Trying to send command for object without authority.
    UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
    BatteryInfo:CallCmdSendBatteryValue(Single)
    BatteryInfo:Update() (at Assets/Scripts/BatteryInfo.cs:17)

    this is my code:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. public class BatteryInfo : NetworkBehaviour {
    8.     [SyncVar]
    9.     public float valorBateria;
    10.  
    11.     private void Update()
    12.     {
    13.      
    14.         if (!isServer)
    15.         {
    16.             valorBateria = (SystemInfo.batteryLevel) * 100;
    17.             CmdSendBatteryValue(valorBateria);
    18.         }
    19.      
    20.     }
    21.  
    22.     [Command]
    23.     public void CmdSendBatteryValue(float b)
    24.     {
    25.         RpcUpdateBaterry(b);
    26.     }
    27.  
    28.     [ClientRpc]
    29.     public void RpcUpdateBaterry(float b)
    30.     {
    31.         valorBateria = b;
    32.     }
    33.  
    34. }
    35.  
    Am i using using the commands and RpcClient wrong?
    I am usin unity 2017