• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Brooke_Godbold · Oct 03, 2022 at 12:51 PM · networkingmultiplayernetworkserver

Unity Netcode - Method not being called

I have a Network Object to hold all of the player's weapons, w$$anonymous$$ch needs to be spawned in seperately as a prefab. I then need to spawn in a starting weapon for that player straight after t$$anonymous$$s, w$$anonymous$$ch is again a Network Object. These need to be seperate methods though, because the same logic to add a weapon can be called during the game when the player picks up a new weapon.

When I run t$$anonymous$$s code, it creates the initial Gun Holder Objects as expected, and the Host/Server player also has their first gun added to the Holder object. But when the client joins, it creates the Gun Holder, but then skips through the UpdateGuns method w$$anonymous$$ch allocates the initial gun prefab, but then still seems to run the second AddNewGun_ServerRpc method, because at that point it throws a null pointer saying that the currentNewGun hasn't been assigned. I've checked t$$anonymous$$s with logging, where on the second player joining it skips logging for UpdateGuns but still logs for AddNewGun_ServerRpc.

Does anyone know what I'm doing wrong here? It must be somet$$anonymous$$ng to do with the order I'm calling the methods combined with where the calls are being run (Server or Client), but I can't wrap my head around t$$anonymous$$s.

 public Transform gunHolder;
 
 public GameObject gunHolderNetworkObject;
 private GameObject gunHolderReference;
 
 public GameObject startGun;
 public GameObject currentNewGun;
 
 private void Start()
     {
         if (!IsOwner) return;
 
         SetUpGuns_ServerRpc();
 
         UpdateGuns(startGun);
     }
 
 public void UpdateGuns(GameObject gun)
     {
         if (!IsOwner) return;
 
         currentNewGun = gun;
 
         AddNewGun_ServerRpc();
     }
 
 [ServerRpc(RequireOwners$$anonymous$$p = false)]
     private void SetUpGuns_ServerRpc(ServerRpcParams serverRpcParams = default)
     {
         var clientId = serverRpcParams.Receive.SenderClientId;
         if (NetworkManager.ConnectedClients.ContainsKey(clientId))
         {
             NetworkClient client = NetworkManager.ConnectedClients[clientId];
             Transform clientGunHolder = client.PlayerObject.GetComponent<PlayerController>().gunHolder;
 
             gunHolderReference = Instantiate(gunHolderNetworkObject, clientGunHolder.position, clientGunHolder.rotation);
             gunHolderReference.GetComponent<NetworkObject>().SpawnWithOwners$$anonymous$$p(clientId);
             gunHolderReference.GetComponent<GunHolderReference>().SetGunHolder(clientGunHolder.gameObject);
         }
     }
 
 [ServerRpc(RequireOwners$$anonymous$$p = false)]
     private void AddNewGun_ServerRpc(ServerRpcParams serverRpcParams = default)
     {
         var clientId = serverRpcParams.Receive.SenderClientId;
         if (NetworkManager.ConnectedClients.ContainsKey(clientId))
         {
             NetworkClient client = NetworkManager.ConnectedClients[clientId];
             GameObject currentNewGun = client.PlayerObject.GetComponent<PlayerController>().currentNewGun;
 
             GameObject newGun = Instantiate(currentNewGun, gunHolder.position, gunHolder.rotation);
             newGun.GetComponent<NetworkObject>().SpawnWithOwners$$anonymous$$p(clientId);
 
             newGun.transform.parent = gunHolderReference.transform;
 
             playerGuns.Add(newGun.GetComponent<Gun>());
             newGun.GetComponent<Gun>().currentAmmo = newGun.GetComponent<Gun>().maxAmmo;
 
             UIController.instance.ShowNotification($"{Gun.gunNames[newGun.GetComponent<Gun>().gunType]}");
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Brooke_Godbold · Oct 05, 2022 at 10:21 AM

I've managed to resolve t$$anonymous$$s, was all about the process flow between methods. Had to rewrite the code to create a logic flow that passes between ServerRpc calls and ClientRpc calls sequentially, with each subsequent call contained wit$$anonymous$$n the previous call to ensure the async Rpc calls get completed before the next one is fired off.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

278 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Networking Player Nametag 1 Answer

Networking for 1v1 rooms 0 Answers

My network server doesn't work. 0 Answers

How to make a simple network server? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges