The question is answered, right answer was accepted
[uNET] Instantiated gameobjects not syncing for new clients
Hello!
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Weapons : NetworkBehaviour {
public bool rif = true;
public GameObject rifleprefab;
private GameObject rifl;
public GameObject rightHand;
[SyncVar]
public bool haswep = false;
[SyncVar]
public GameObject weapon;
[SyncVar (hook = "OnWeapon")] public NetworkIdentity hasRifle;
// Use this for initialization
void Start () {
if(isLocalPlayer)
{
ClientScene.RegisterPrefab(rifleprefab);
ClientSpawn();
}
}
[Client]
void ClientSpawn()
{
if (isLocalPlayer)
{
if (!haswep)
{
CmdSpawnRifle(transform.position, transform.rotation, rightHand);
haswep = true;
}
}
}
void OnWeapon(NetworkIdentity hasweapon)
{
weapon.transform.SetParent(rightHand.transform);
weapon.transform.localPosition = new Vector3(0.0896f, -0.0204f, 0.0275f);
weapon.transform.localRotation = Quaternion.Euler(-29.4f, 111.3f, 86.617f);
}
[Command]
public void CmdSpawnRifle(Vector3 p, Quaternion r, GameObject g)
{
GameObject rifle = Instantiate(rifleprefab, rightHand.transform.position, transform.rotation) as GameObject;
NetworkServer.SpawnWithClientAuthority(rifle, base.connectionToClient);
weapon = rifle;
hasRifle = gameObject.GetComponent<NetworkIdentity>();
}
public void Fire()
{
}
}
With this code, when a new player connects, it actually spawns the gameobject for himself and for other players, but the gameobject isn't parented (so it just floats there). Whereas, for the already connected clients, it works perfectly. If the other clients respawns, it will work.
I don't know at all how I should correct that, there might be something wrong in my code. Any idea how to fix it please?
Surely but how should I sync that? With a SyncVar? But I will need a Cmd no? Edit: And thank you btw
Follow this Question
Related Questions
UNet - sync child transform scale 1 Answer
Unet Syncing Flashlights 2 Answers
how to split player objects over network? 0 Answers
Unity 5.1 networking 0 Answers
Problem with using Unity Spawnsystem (Unet / Multiplayer) 0 Answers