I have a class attached to each player. When the player is spawned I assign the player a name (just the datetime of spawning for now) and a random sprite. As I create more clients the sprites and names do not show to the other clients. What am I missing?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMove : NetworkBehaviour
{
[SyncVar]
public Sprite mySprite;
void Update()
{
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal") * 0.1f;
var z = Input.GetAxis("Vertical") * 0.1f;
transform.Translate(x, 0, z);
gameObject.GetComponent<SpriteRenderer>().sprite = mySprite;
}
public override void OnStartLocalPlayer()
{
UpdateServerWithMyInfo();
}
[Command]
void UpdateServerWithMyInfo()
{
mySprite = GameObject.Find("SpriteLibrary").GetComponent<SpriteChooser>().GetNextSprite();
gameObject.GetComponentInChildren<TextMesh>().text = DateTime.Now.ToString();
}
}
I’m guessing I do not understand what I’m doing when it comes to using SyncVar and [Command]. But I thought I’d make the Sprite a SyncVar, and when it was changed the server and all clients would know about it. And onspawning, a new sprite would be assigned to mySprite (the SyncVar) so… I thought this would work.
Sprite is not a type which can be synced as a SyncVar, as far as I’m aware. I’d send another identifier for the sprite and then set the correct sprite on each client based on that identifier.
I don’t see where you are trying to sync the player name to any other the other clients in the above code. You are setting it on a TextMesh on the server, but that won’t be synced to any of the clients unless you add code to do so.
That’s helpful thank you. I’ve updated my code to have an object that gets spawned when the server starts. It is on a prefab with an array of sprites, and the intent is to give a new player their own sprite.
public Sprite[] Sprites;
[SyncVar]
public int AvailableSpriteId;
public int GetNextSpriteID()
{
if (isServer)
{
AvailableSpriteId++;
if (AvailableSpriteId > 5)
AvailableSpriteId = 0;
}
return AvailableSpriteId;
}
And this code is on the player prefab
[SyncVar]
public int MySpriteID; // SyncVar assures that each MySpriteID is sync'd on the server
void Update()
{
gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.FindGameObjectWithTag("SpriteLibrary").GetComponent<SpriteChooser>().Sprites[MySpriteID];
}
public override void OnStartLocalPlayer()
{
ObtainMyInfoFromServer();
base.OnStartLocalPlayer();
}
void ObtainMyInfoFromServer()
{
GetAvailbleSpriteID();
}
void GetAvailbleSpriteID()
{
SpriteChooser goSpriteLibrary = GameObject.FindGameObjectWithTag("SpriteLibrary").GetComponent<SpriteChooser>();
MySpriteID = goSpriteLibrary.GetNextSpriteID();
}
I would expect the following
Player spawns in
Player asks SpriteLibrary for an integer, it is saved in the PlayerPrefab as a SyncVar Int
SpriteLibrary increments +1, ready for the next player to spawn.
Instead, depending on the client you are on, you look different than the rest, but the other players have not been changed. Any idea what I’m missing?