Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by Paincho · Nov 09, 2015 at 02:17 PM · errornetworkingserverrpcserverside

[Solved]NullReferenceException in a ClientRpc method

Hello there.

I'm trying to make a coop game and here is my issue. I have two functions that I call to indicate if a player has equipped an item so that both players can see visually those items equipped each other characters.

Here is how I call those functions :

 if(!isServer){
     CmdItemEquipped(eItems[itemName].GetComponentInParent<NetworkIdentity>().netId,eItems[itemName].name);
 }else{
     RpcItemEquipped(eItems[itemName].GetComponentInParent<NetworkIdentity>().netId,eItems[itemName].name);
 }

Basically if I am not the server I'm calling the CmdItemEquipped so that the server can see that I equipped an item and indicate it visually to itself and the other clients. That's working. The second part is if I'm the server; then I'm calling the RpcItemEquipped function that does the same thing as the first function but is sent to the clients to indicate them to visually equip a weapon on the server's player. But the second function does not completely work. Here are those two functions :

     [Command]
     void CmdItemEquipped(NetworkInstanceId playerID,string itemName){
         GameObject player = NetworkServer.FindLocalObject (playerID);
         Debug.Log (playerID + " found");
         Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);
         Debug.Log (items.Length+" items trouvés");
         foreach (Item item in items) {
             if(item.gameObject.name==itemName){
                 item.gameObject.SetActive(true);
                 Debug.Log(itemName+" activated");
                 break;
             }
         }
     }
 
     [ClientRpc]
     void RpcItemEquipped(NetworkInstanceId playerID,string itemName){
         GameObject player = NetworkServer.FindLocalObject (playerID);
         Debug.Log (playerID + " found");
         Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);
         Debug.Log (items.Length+" items trouvés");
         foreach (Item item in items) {
             if(item.gameObject.name==itemName){
                 item.gameObject.SetActive(true);
                 Debug.Log(itemName+" activated");
                 break;
             }
         }
     }

The first one works as intended. I pass the NetworkInstanceId as an argument to be able to retrieve the player's gameobject then I pass a second argument which is the item name. With those I'm able to find the player's gameobject on the server then retrieve the item the player equipped and activate it so that it appears visually.

On the second function the player is found with the NetworkInstanceId the Debug.Log(playerID + "found") confirmed it but the function can't seem to be able to find the items on the server's player gameobject and the client is unable to see visually what the server's player just equipped.

Here is the error I get in the console :

 NullReferenceException: Object reference not set to an instance of an object
 PlayerAction.RpcItemEquipped (NetworkInstanceId playerID, System.String itemName) (at Assets/Scripts/PlayerAction.cs:150)

It points out to this line of code :

 Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);

I can't find what's wrong with it since the first function works perfectly plus the player is found so the player variable is not null but when I try to get the items from it I get a NullReferenceException.

The weird thing is I saw that the items were there in the inspector when I'm starting Unity as the client.

If anyone as any hint or suggestion it would be a great leap forward for me.

Thanks.

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
1
Best Answer

Answer by Paincho · Nov 09, 2015 at 05:39 PM

I found the issue.

I am using the NetworkServer on the client which obviously now that I think about it is idiotic since NetworkServer is only present on... well... the server.

Now I need to find another way to tell the client that my server's player changed weapon.

SOLUTION if anyone is wondering. You can use ClientScene.FindLocalObject() to return a gameobject from a networkid.

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.

Follow this Question

Answers Answers and Comments

42 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

Related Questions

Spawning Network Objects from the client | Unity Netcode For Gameobjects 0 Answers

Can Photon Unity Network be used for MMO prototype? 0 Answers

Failed to spawn server object, assetid=... 1 Answer

How to change turns on button click using HLAPI? 0 Answers

Client can't answer to Server, Command methods don't work, ClientRPC do 0 Answers


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