- Home /
Dices over Network
Hi everyone, I've got an big issues, and I spend days to find an answer but nothing works.. So at the beginning of my game, I've got an small mini-game where everyone roll a dice to decide which player is the first.
And I found no way to synchronise the number with every client! I tried, RPC, Sync Hook, everything I found on the internet.. Here is my current not working script..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class RandomDice : NetworkBehaviour {
public Text Number;
//[SyncVar(hook = "TextOnServer")]
public string RndmNumber;
private NetworkIdentity assignAuthorityObj;
public void Start()
{
assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);
}
public void RandomNumber(){
CmdTextOnServer(Random.Range(1, 7).ToString());
}
private void Update()
{
Number.text = RndmNumber;
}
[Command]
void CmdTextOnServer(string DiceNumber){
RpcUpdateNumber(DiceNumber);
}
[ClientRpc]
void RpcUpdateNumber(string DiceNumber){
Debug.Log(DiceNumber);
RndmNumber = DiceNumber;
Number.text = RndmNumber;
NetworkServer.Spawn(gameObject);
}
}
It would be very kind of you to explain to me, show me exemples, to make it work.. thank you very much. :D
Answer by DreamingWorld · Jul 12, 2018 at 06:42 AM
Okay, don't ask me why, but it kind of worked like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class RandomDice : NetworkBehaviour {
public Text Number;
//[SyncVar(hook = "TextOnServer")]
public string RndmNumber;
private NetworkIdentity assignAuthorityObj;
public void Start()
{
assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);
}
public void RandomNumber(){
CmdTextOnServer(Random.Range(1, 7).ToString());
}
private void Update()
{
Number.text = RndmNumber;
NetworkServer.Spawn(gameObject);
if (transform.position.y < 2){
Destroy(gameObject);
}
}
[Command]
void CmdTextOnServer(string DiceNumber){
RpcUpdateNumber(DiceNumber);
}
[ClientRpc]
void RpcUpdateNumber(string DiceNumber){
Debug.Log(DiceNumber);
RndmNumber = DiceNumber;
Number.text = RndmNumber;
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
using Switch and Case for 50% chance of occurance? C# 3 Answers
Teleporting camera and gameobjekt togather at random? 0 Answers