The question is answered, right answer was accepted
Unet NetworkServer.spawn, correcting my workflow: Found no behaviour for incoming Command
I've been trying to get spawning non player objects right and after searching through other asks on the subject I have come to a bit of a standstill in the current state I have two problems, first only the host is able to spawn the object, second only the host is able to see the visual modifications to the spawned prefabs(velocity and position are fine).
My workflow and assumptions are as follows
Vanilla player spawn with no special modification to the network.
What is essentially a factory is added to the player gameobject (assumption : to piggy back off of the player's client authority), each player getting their own factory.
The player makes a call to the factory
The factory makes a call to it'd own [command] function
The factory command instantiates a projectile controller The factory command networkserver spawns the projectile controller The factory command passes values to the projectile controller
The projectile controller takes in all of the passed variables
The projectile controller calls it's own command (assumption: this is redundant because it should already be on the server because of the factories command)
The projectile controller command sets it's position, velocity and spawns the prefabs that make the projectile visual and modifies them
I assume that to fix the problem of visuals I need to extract the prefab modifying calls from the command and have the spawning script send a message to all of the clients to call that new function, though I haven't looked into sending messages yet. I am however, not sure where to go from here on the ability of only the host to spawn projectiles.
Edit 1: After testing with the unity editor as the host client the following error appeared in the
Found no behaviour for incoming [Command:InvokeCmd] on 3rd Person Controller
It appears that based on the error wording that the host can't find the command on it's side. Based on searching that I did the problem is that the other object wasn't spawned on the server. But, I checked in the Editor's hierarchy view and both available player prefabs had the factory script attached.
Edit 2: I attempted to add a command to the player controller that sets the variable that points to the currently used factory. After adding the command and replacing the relevant attempts to set the variable with the command version the Unity editor crashes when a client attempts to connect with an error along the lines of There is a player with this playercontrolid already
Any help would be appreciated.
Answer by Pafk · Apr 01, 2016 at 11:27 PM
Solved the more important of the two problems, the ability of the host to recognize commands from the clients.
For anyone that is trying to do commands from abstracted classes like me, make sure that the script calling the command is attached to your GameObject in the Awaken function and not the Start function, otherwise it seems that the server does not recognize the two scripts as being the same.
My finished and fixed workflow is as follows
Player is spawned
Factory is created in the Awaken
User presses correct command
Factory's function is called
Factory calls it's command passing in variables with player location and velocity (this is a fast paced action game so it is important to player immersion that when they shoot their projectile that it fires out from their location and direction, if I relied on the server location a player moving forward and shooting their projectile would/could hit themselves with it, since server-side interpolation would make the player shoot the projectile from where they were 1-2 seconds ago, trying to fix this by changing interpolation settings would result in very, very bad jitters to the point where one player looked like two players next to each other)
Factory's command spawns the projectile base prefab
In the projectile prefab's Awake function it attaches the collider and renderer objects to the projectile GameObject
The Factory calls the projectile's variable setting function and sets relevant SyncVars
In the Start function the size of the collider and renderer are set based on the SyncVars set in the previous step
To help anyone else having similar problem it seems that as far as the initialization process over the HLAPI goes, Awaken is called only on the original client/host, and that Start is called on all clients on which the object is spawned. This is important to solving both parts of this problem.
The script needs to be added before being propagated across the network so that all clients recognize that script as the same for each player.
For the rendering problem it is important because Start is not called until after the current frame is over, this means that you can set variables that will be used by Start in the same frame as you Initialize it.
$$anonymous$$oving my NetworkBehaviour Creation on Awake solve my problem! Thanks!