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 /
avatar image
3
Question by Trekopep · Oct 14, 2016 at 06:10 PM · unity 5networkingbugrpcinheritance

UNET Rpc calls wrong inherited script

Hi All,

So here's the setup: I have a GameObject with a Network Identity and Local Player Authority. I have a base class and two child classes that extend the base class. The base class makes an Rpc call.

I want to put both of these child classes on the same object, but when I do that, the Rpc call executes on the first class attached to the object, rather than the one that made the call.

Of course, I could have a child object with a separate NetworkIdentity store one of the Child classes, but I wanted to know whether this is a bug in Unity, or if it's intended behavior, or if I'm just doing something wrong.

The "Base" class looks something like this:

 using UnityEngine;
 using UnityEngine.Networking;
 
 public abstract class Base : NetworkBehaviour {
 
     // The client's player object
     public GameObject player;
 
     public void Execute() {
         if(player.GetComponent<NetworkIdentity>().isServer)
         {
             RpcExecute();
         } else {
             CmdExecute();
         }
     }
 
     [Command]
     public void CmdExecute() {
         RpcExecute();
     }
 
     [ClientRpc]
     public void RpcExecute() {
         Actions();
     }
 
     public abstract void Actions();
 }

And the two sub-classes, ChildA and ChildB, both look like this:

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class ChildX : Base {
     public override void Actions {
         // Execute some actions here
     }
 }

Any feedback is appreciated!

Comment
Add comment · Show 2
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
avatar image MDesautels · Mar 09, 2017 at 08:27 AM 0
Share

Up please? Seriously this is very annoying

avatar image Svennn · Apr 09, 2017 at 06:50 PM 0
Share

Same problem here, Example setup: Spell is base class, HealSpell and DamageSpell extend Spell. Both heal and damage spell are attached to the Player object. Spell class contains an RPC from server to client, but if the damagespell is the subclass, the RPC in spell will still be exectued on the client side on the healspell instance. (the first one it encountered.)

This is a very annoying and seems like a huge bug to me.

Anyone has a solution for this?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by EAST-DATA · Apr 10, 2017 at 09:10 AM

 public int rpcId;//Specific id
 private int mRpcExecuteHash ;
     private string mRpcExecuteName ;
     private NetworkWriter mRpcExecuteWriter = new NetworkWriter();
 
 public override void OnStartClient()
      {
             base.OnStartClient();
             mRpcExecuteName = "RpcExecute:" + rpcId.ToString() + netId.ToString();
             mRpcExecuteHash = mRpcExecuteName .GetHashCode();
             RegisterRpcDelegate(GetType(), mRpcExecuteHash , RpcExecute);
         }
     
         public override void OnStartServer()
         {
             base.OnStartServer();
             mRpcExecuteName = "RpcExecute:" + rpcId.ToString() + netId.ToString();
             mRpcExecuteHash = mRpcExecuteName .GetHashCode();
         }
     [Command]
      public void CmdExecute() {
          mRpcExecuteWriter .StartMessage(MsgType.Rpc);
         mRpcExecuteWriter .WritePackedUInt32((uint)mRpcExecuteHash );
         mRpcExecuteWriter .Write(netId);
         //mRpcExecuteWriter .Write(some more data);
         mRpcExecuteWriter .FinishMessage();
         SendRPCInternal(mRpcExecuteWriter , 0, mRpcExecuteName );
      }
 
 void RpcExecute(NetworkBehaviour obj, NetworkReader reader)
     {
        //read data
     }


Not the best solution, but it works for me. Thanks.

Comment
Add comment · Show 1 · 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
avatar image CompediaDev · Apr 27, 2018 at 01:48 AM 0
Share

This was very helpful, and actually got my project back on track. Thanks a lot!

On a side note: Ins$$anonymous$$d of exposing the RPC ID to the inspector, you could use the index of this component instance in your object's components, and therefore assuring that each instance of the derived script on your object has a different ID that is consistent in the structure of your prefab.

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

120 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

Related Questions

Changing variable value Network 1 Answer

How to send player reference over network 1 Answer

Why do I keep getting UNetWeaver error: GetWriteFunc recursion depth exceeded for NetworkInstanceId 2 Answers

OnDisconnectedFromServer ignores inheritance 1 Answer

Using command attribute in child classes 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