Hi all,
I’m trying to develop a multiplayer project and I have a problem with spawning GameObjects on client.
Here is the code I use:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class WeaponController : NetworkBehaviour
{
[SerializeField] private GameObject firePosition;
[SerializeField] private ShotEffectsManager[] laserGuns;
[SerializeField] private ShotEffectsManager rocketLauncher;
float nextFire;
float nextEffectFire;
float nextRocketFire;
private bool canShoot;
bool laserShot = false;
bool rocketLaunched = false;
void Start()
{
for(int i =0; i < laserGuns.Length; i++)
{
laserGuns*.Initialize();*
}
rocketLauncher.Initialize();
if (isLocalPlayer)
canShoot = true;
}
void Update()
{
if (!canShoot)
return;
if (Input.GetButton(“Fire1”) && Time.time > nextFire)
{
nextFire = Time.time + laserGuns[0].fireRate;
laserShot = true;
rocketLaunched = false;
CmdProcessShotEffects();
}
if (Input.GetButton(“Jump”))
{
rocketLaunched = true;
laserShot = false;
CmdProcessShotEffects();
}
}
[Command]
void CmdProcessShotEffects()
{
if (!NetworkServer.active)
{
Debug.Log(“SERVER IS NOT ACTIVE!!”);
return;
}
else
{
Debug.Log(“Server Active.”);
}
if (laserShot)
{
if (Time.time > nextEffectFire)
{
nextEffectFire = Time.time + laserGuns[0].fireRate;
for (int i = 0; i < laserGuns.Length; i++)
{
Debug.Log(“Before instantiating”);
laserGuns*.SetShotEffects();*
GameObject go = Instantiate(laserGuns.ammoPrefab, laserGuns_.transform.position, laserGuns*.transform.rotation) as GameObject;*
Debug.Log("Laser: " + go);
NetworkServer.Spawn(go);
Debug.Log(“Networked Spawned”);_
}
}
}
if (rocketLaunched)
{
rocketLauncher.SetShotEffects();
if (Time.time > nextRocketFire)
{
nextRocketFire = Time.time + rocketLauncher.fireRate;
Debug.Log(“Before instantiating Rocket”);
GameObject instance = Instantiate(rocketLauncher.ammoPrefab, rocketLauncher.transform.position, rocketLauncher.transform.rotation) as GameObject;
Debug.Log("Rocket: " + instance);
NetworkServer.Spawn(instance);
Debug.Log(“Networked Spawned Rocket”);
}
}
}
}
I’ve watched tutorials and added a lot of Debug.Logs in order to see what’s happening there and it seems that CmdProcessShotEffects() is never called on clients I think. Everything is done right: both laser and rocket prefabs are spawnable prefabs in the NetworkManager and both have NetworkIdentity and NetworkTransform components.
I also have an unusual warning that sais:
" Did not find target for sync message for 14 //This number is different every time I shoot
UnityEngine.Networking.NetworkIdentity: UNetStaticUpdate() "
And I don’t know what it means.
Can anyone help me understand this issue, please?