[UNet] Shooting weapon

Hello, i’m havnig troubles with sending a command from player’s top object down to the gun object in his hands.

So the hierarchy of the player prefab is this:

ROOT (With PlayerControl script)
--SkeletonRoot
----Hips
------<...>
--------Wrist
----------Guns
------------Gun1
------------Gun2
------------Gun3

Currently I implemented shooting through the Weapon script attaches to a gun. So I can have different weapons doing different things, and I won’t need to adjust the player’s top object. For example I plan to have a simple shooting gun, a gravity gun, a particle gun, etcetc, all doing different things.

So the Weapon script of the Gun currently tracks player’s input, particularly RMB for aim and LMB for shooting, it also runs the appropriate animation of the player object for this particular gun.

So, just saying, trying to making it somehow modular. And yes, I’m not an experienced programmed, so it can be a wrong approach in it’s core, of course.

But the particular problem I have is this:
I’m trying to use UNet to network this. And right now I can’t call [RPC] function from the weapon. And I can’t assing Authority of it to the player, because it is a part of player prefab which is required to have only one NetworkIdentity script on it.
I tried calling [Command] from the PlayerControl script, to track player’s input only there, but that did not work either. Because to call a command from object it has to be Network Identity too.

So the problem is - How do I call a Shoot command from a child object of the Player??

Thanks in advance.

Do you have a manager script on the “guns” bone? Some sort of script that is managing what gun is displayed/firing? If not, you should. You could have a direct reference to your player script in that manager. Whenever the gun is shot call a function inside the player script. (im guessing you have some sort of function to play sounds/raycast/instantiate bullets/whatever)

playerScript.Shoot();

Inside your player script where you are calling RPC commands put whatever you need into the “Shoot” function.

public void Shoot(){
//rpc stuff
}

If you want to have a unique script on each gun, thats your choice, but there are more efficient and time saving ways to set up a gun using inheritance. They are all guns, they just shoot differently/cause different amounts of damage/etc, but they do primarily the same thing. If youre going with this “script-per-gun” strategy, when the gun is activated have it make sure it has a reference to the “gunManager” script you have attached to the “Guns” bone. When the gun is shot, it tells the gunManager, which in turn can call the “Shoot” function on the player.