Alternative for this code?

Hello!

I wanted to know if there was another way to do this code so that clients on a UNET server could spawn objects:

NetworkServer.Spawn (object);

I already have found out clients cannot execute that code. But, I need all clients to be able to throw grenades, and shoot their gun (The gun is a child of them by the way) and for the enemy manager to spawn enemy prefabs (All the spawn objects are Network Manager registered). Based on my information given, what is a solution?

EDIT

Also, I’d like to include the error I get when a client does run that cod":

Failed to spawn server object, assetId=21a17664858912f4f873e24713a4ef81 netId=25
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

@Kiwasi hi, I know it’s been a while, but could you take a look at this thread?

Hi. This is probably a scripting forum of networking forum rather then a discussion thread.

The typical solution is to send commands to the server, and have the server spawn the objects.

Check out this page in the docs and play close attention to the picture in the page. It’s crucial to understanding UNet.

http://docs.unity3d.com/Manual/UNetActions.html

@Kiwasi thanks for replying. I want to start this comment by apologizing for the inconvenience of me posting here, but I didn’t know which category it would fall under.

Unfortunately, I already looked at that. I’m still a bit confused on which combination of attributes and functions to use in order to not be thrown any errors.

I’m not sure if it works anything like Photon as I’ve never used UNet. But if UNet anywhere resembles the way the old networking, then it would be something along the lines of this (I’m going to use Photon as that’s the only thing I know how to use).

if(photonView.isMine){
// Old Unity Networking would be if(networkView.isMine) I believe.
// Run your spawn code for bullets here.(Or whatever they are).

PhotonNetwork.Instantiate(SpawnPosition.position,SpawnPosition.rotation,0);

// In OLD unity Networking, just replace PhotonNetwork with //the main class for networking. Don't know UNet though.
// Should be close to it. But I'm just trying to help
// None of this may work for your needs
// Just depends how good you are at converting what you
// Want to what you need with what I gave you.

}

@N1warhead_1 I’m not sure if it’s the same, but I know if I want something to be visualized by everyone you have to do a NetworkServer function. I’m looking but I don’t know exactly what to do.

Does anyone know about NetworkIdentity.AssignClientAuthority () or NetworkServer.SpawnWithClientAuthority ()?

I think these could be my answer.

It will work somewhat the same way.

the if(photonView.isMine) or in your case probably if(networkView.isMine){}

means it will run the code only on your client.

But if you want to do it the proper way do an RPC call
to send the desired code over the network.

Like I said, UNet I’m sure works a bit differently than Photon or old Unity Networking, however, An RPC is an RPC, and it will always work as an RPC.

So learn about UNet RPC Calls and you will get it to work.
The whole Server.Spawn thing chances are is instantiating a object over the network, which isn’t what you’d want. (I could be wrong as said I don’t know UNet) however, an RPC Call is definitely the desired thing for instantiating anything that won’t survive on the Network long.

An RPC will just send out the location at which it was shot at, and then run the logic of the item on each client (Locally).
Doing something like shooting a bullet over the Network and having the Network Sync it will kill network performance quickly because it’s trying to send like 10 messages per second roughly to keep it Synced, However if you use an RPC, it sends out one message to each client, and then the logic of the object is calculated over each client Locally.

Example 100 bullets over Network would be around 1000 messages a second.(1 bullet = 10 message p/second.)
However, 100 bullets on each client locally would be 100 messages on each client. (1 bullet = 1 message in total).

I hope that makes sense?

I just somewhat fixed it.

But I have a new error

AssignClientAuthority can only be call on the server for spawned objects.
UnityEngine.Networking.NetworkIdentity:AssignClientAuthority(NetworkConnection)
Player:Start() (at Assets/Scripts/Player.cs:65)

from adding this line in start in the player script, but there was 1000+ of the same errors now theres just 9.

if (!isServer)
{
GetComponent ().AssignClientAuthority (GetComponent ().connectionToClient);
}

@N1warhead_1 that’s a helpful tip thanks! I’m still struggling with this though. I changed all the spawn codes to:

NetworkServer.SpawnWithClientAuthority (b, player.GetComponent ().connectionToClient);

and the player now has this in the start:

if (!isServer)
{
GetComponent ().AssignClientAuthority (GetComponent ().connectionToClient);
}

It didn’t fix it, but it reduced the amount of repetitive errors from 999+ to 9, which is better than nothing.

However, I have another error on a client.

AssignClientAuthority can only be call on the server for spawned objects.
UnityEngine.Networking.NetworkIdentity:AssignClientAuthority(NetworkConnection)
Player:Start() (at Assets/Scripts/Player.cs:65)

You should really use this Unity - Scripting API: NetworkView.isMine
This link may be for old Networking (It doesn’t Say!).
If I am correct using if(isServer) (if it works like the other networking) if you shoot, it will shoot on all clients except the master of the server.

Now if you are the master of the server it won’t shoot unless you use if(isServer) without the ! in it.

However if you use the thing I referenced it won’t matter who is in the server, so long as the network view is yours it will use only your code and nobody elses.

I can’t really say what any of the stuff means that you typed because I’ve never used UNet for more than 5 minutes (At the time it was buggy when it first came out) so I decided to hold off on it.

But I’m telling you from expierience just use an RPC.
It’s very simple to use an RPC.

I believe this is the same thing as an RPC from what I can read of it.
http://docs.unity3d.com/Manual/UNetActions.html

So I would look into the above link above this.
otherwise there’s not really much more I can help with bud, sorry.

@N1warhead_1 Yea that is a legacy document.

Although this isn’t about bullets I will take your suggestion because it is in fact very helpful.

Thanks for the help you could offer. I guess I’ll try to find my answer elsewhere.

Well it doesn’t have to be about bullets. It’s just better to use RPC’s for ANYTHIGN that doesn’t need to last long on the server (E.G. - Something that is not a player or a vehicle that can be controlled by the player). But something lasting E.G> - 10 seconds needs an RPC. Something that just needs to be seen by other players but not synced with 10 msgs per second over the the net…

But yeah, sorry man I tried to help :frowning:

The UNet model is slightly different from the old networking model. Clients aren’t able to send RPCs directly.

Clients send Commands to the server. The server then sends ClientRPCs out to the clients.

Hey guys,

I fixed the issue. The reason there were so many errors is I have a custom particle system and I didn’t register particles. I’m having another error though. Its a simple one. I just don’t know what the meaning of it is. If I post it can someone tell me?

Don’t see why they would change RPC to Command, I figured that’s what that one page meant when I showed him about the commands, looked exactly like it worked like an RPC. But guess it was an RPC but called Command, why fix something that’s not broke though, the whole world is used to using RPC so it seems more confusing lol.

You can still use a low level API in a more traditional manner.

The differention between Commands and RPCs is on the high level layer. The high level layer is designed to make networking accessible for non expert programmers. Splitting Commands from RPCs makes the whole system simpler to conceptualise.

Yeah I see what you mean.