C# UNET Instantiate over network not working, tried [ClientRpc] & [Command] what's missing/incorrect?

Im using 2 clients to connect over a network. Only one client is a host, but both are played by a person in the same way.

My code is working 100% for creating moving and synchronizing my players and their variables.

Then when i instantiate objects offline, they spawn properly.

If i convert Instantiate code into Network.Instantiate…things don’t work right, or at all.

I registered my object to spawn on my Network Manager in the inspector.

what am i doing wrong?

In my update:
if(Input.GetAxis ("Attack") == 1){
			if(Time.time > attackTimer){
				CmdSpawnObject(dummyPrefab, transform.position + transform.forward * 1, Quaternion.identity);
				//counter++;
				attackTimer = Time.time + 0.05f;
			}
		}
My Instantiate function:
[Command]
	void CmdSpawnObject(GameObject newObj, Vector3 pos, Quaternion rot){
		if(isLocalPlayer){
			GameObject ob = (GameObject)Instantiate(newObj, pos, rot);
			NetworkServer.Spawn(ob);
		}
	} 

EDIT:
Ok, I changed my CmdSpawnObject(); and it now only contains the line:
Instantiate(newObj, pos, rot);

There was no point in using NetworkServer.dostuff inside a network function
and i assume the objects that represent my player on the other peoples clients will never be considered as (isLocalPlayer)

Result: This fixed the object to spawn properly on my hosts, and when i attempt to spawn on a client it sends an error(which is good because something obviously came from client to host now) but it says null reference on my object to be spawned, why would it do that to my client and not my host?

1 Answer

1

hi… Actually i’m also working on unity3d networking concept only. First of all i would say thanks to introduce me a new method of unity3d. i found a solution for ur problem in 1 . once check this,. and i’ll use directly network.instantiate.it is easy if i’m right…let me know the result plz.

Thanks for replying, ill check that out.

try with Network.Instantiate instead of NetworkServer.Spawn(ob).

Ok i tried what you linked and it works perfect for a game that has a dedicated server because it spawned properly on the server and updated for the clients but not vice-versa. It is a good find though. If i cant fix my code, i might just have to make a dedicated server and use this instead.

I tried Network.Instantiate and nothing spawned at all. I got a null reference error from client side, and i got a "not connected" error on the server.

oh...but for me it worked...i'm not good enough in networking,.i'm trying basic concept of network.if my answer is ok for u hit up..let me know if u want more help.