Enabling Gameobjects over Network uNET

I am trying to do a pretty simple thing, and I finally thought I some what understood Networking, but guess not hehe.

I have a fully functioning FPS multiplayer game thanks to Unity Fragmas 3.0 tutorial series!

I was trying to implement a simple weapon pick up. So basically i added a gameobject and disabled it, and wanted to enable it for that player once you collided with that object on the level and disable the other weapon.

I made it even simpler for testing that if you press “r” it would just swap between the 2 weapons regardless of pickup. But that doesn’t seem to work either.

Here is my code for Networking:

//CODE-----------------------
[Command] void CmdChangeWeapons() {
bool rifleState;
bool rocketState;
if(Rifle.activeSelf)
{
rocketState =true;
rifleState =false;
}
else
{
rocketState =false;
rifleState =true;
}
RpcChangeWeapons(rifleState, rocketState);
}
[ClientRpc] voidRpcChangeWeapons(boolRifleState,boolRocketState)
{
Rifle.SetActive(RifleState);
RocketLauncher.SetActive(RocketState);
if(RifleState)
gunSyncScript.ChangeToRifle();
else
gunSyncScript.ChangeToRocketLauncher();
}

I do not see the issue with your code. You could try adding Debug.Log(…) to the code to see what is (and isn’t) being called.

So is the logic correct here? is this how you would go about enabling and disabling objects in the hierarchy over network?

Add Debug.Log() to various sections of your code and see where it’s breaking down.
-Is the button R being detected on the client?
-Is the command being received on the server
-Is the RPC being received on the client
-Is the gunSyncScript.ChangeToRifle script being called?

Figure out where it’s breaking down.

I suggest you send over the networkinstanceid (netId) when sending commands and trying to disable and enable other objects with rpc call, it may not return the correct gameobject or even return null in some cases, so basically send the netId to the server and to the rpc aswell, define the target with ClientScene.FindLocalGameObject(input_netid) and define a path to the weapon slot and enable it.

1 Like

@Hardstop I will try what you said, although they always discourage using “FindGameObjects”.

as for debugging, how do i debug over network. I.E. if a client presses R how would i make sure that message got across the network.

Do not mix up the usual GameObject.Find, FindGameobjects, etc. functions (relatively slow, not ideal choices in some cases) with NetworkServer/ClientScene.FindLocalObject. These work differently and the latter is quite as fast as fetching a value from a dictionary. It is the usual way to identify networked objects across server and clients. So, there should not be any discourage for using.

Ok sounds good. I will most definitely try this and get back to you guys!

Thank you!