Hi;
I’m working on my first multiplayer project, and I ran into an issue:
In one of my scripts, when on the Client side, it doesn’t enter the Cmd code (to spawn an object).
When on the server, everything works fine, the object is spawned and detected from all sides.
The script is on my Player object, that has a Network Identity (local player authority checked; the spawned object’s Network Identity has everything unchecked) , a Network Transform, and another script that uses Commands and works fine
I don’t get any error messages either, but any print()s inside of the Command function are ignored, the client just skips that part.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class AttackChangeCol : NetworkBehaviour
{
RaycastHit hit;
public float hitDistance = 1;
Vector3 offsetPos;
GameObject hitterPrefab;
// Use this for initialization
void Start()
{
offsetPos = new Vector3(0, .5f, 0);
hitterPrefab = (GameObject) Resources.Load ("AttackChangeColHitter");
}
[Command]
void CmdSpawnHitter(){
print ("instantiating hitter");
GameObject hitter = (GameObject) Instantiate (hitterPrefab, hit.transform.position, Quaternion.identity);
NetworkServer.Spawn (hitter);
print ("hitter spawned");
Destroy (hitter, 1f);
}
// Update is called once per frame
void Update()
{
Debug.DrawRay(transform.position + offsetPos, transform.forward * hitDistance, Color.green);
if (Input.GetKeyDown(KeyCode.Space) && isLocalPlayer)
{
if (Physics.Raycast(transform.position+offsetPos, transform.forward, out hit))
{
if (Vector3.Distance(hit.transform.position, transform.position) <= hitDistance)
{
print ("looking");
if (hit.transform.CompareTag("Player"))
{
print ("try change col");
CmdSpawnHitter ();
print ("done");
}
}
}
}
}
}
For example, I see “looking” (line 42), “try change col” (45) and “done”(47), but not “instantiating hitter” (24) or “hitter spawned” (27).
I have watched several shooter tutorials and read documentation, but I still couldn’t figure out what the issue was.
Thanks in advance for your help.