• 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 pzeronov · Apr 30, 2015 at 09:20 AM · networkserverrpcinstanceclient

RPC is called but it doesn't destroy some GameObject instances

I have some multiplayer code that it's giving me a bit of a headache. I'm not synchronizing anyt$$anonymous$$ng over the network as I only need to transfer data between client and server. Only two players can connect (server + client).

When I set up a server and a client connects to it, some prefabs are instantiated in both client and server:

 void OnConnectedToServer()
     {
         print("Server Joined");
         
         // ASK THE SERVER FOR ALL PARAMETERS NEEDED FOR OTHER PARTY RECONSTRUCTION
         nV.RPC("Request", RPCMode.Server);
 
         isConnected = true;
         
     }
 
 [RPC]
     void Request(NetworkMessageInfo info)
     {
         print("SERVER: I just got a request from a client that wants to meet with me");
 
         // spawn units in SERVER
         _enemyUnitOne = (GameObject)Instantiate(enemyUnitOne, _posOne, Quaternion.identity);
         _enemyUnitTwo = (GameObject)Instantiate(enemyUnitTwo, _posTwo, Quaternion.identity);
         _enemyUnitThree = (GameObject)Instantiate(enemyUnitThree, _posThree, Quaternion.identity);
         _enemyUnitFour = (GameObject)Instantiate(enemyUnitFour, _posFour, Quaternion.identity);
 
         // trigger enemy units spawn in CLIENT
         nV.RPC("PlaceUnits", info.sender);
     }
 

That's good so far. The PlaceUnits function looks like t$$anonymous$$s:

 [RPC]
     void PlaceUnits()
     {
         print("CLIENT: placing units...");
 
         _enemyUnitOne = (GameObject)Instantiate(enemyUnitOne, _posOne, Quaternion.identity);
          _enemyUnitTwo = (GameObject)Instantiate(enemyUnitTwo, _posTwo, Quaternion.identity);
          _enemyUnitThree = (GameObject)Instantiate(enemyUnitThree, _posThree, Quaternion.identity);
          _enemyUnitFour = (GameObject)Instantiate(enemyUnitFour, _posFour, Quaternion.identity);
 
         print("CLIENT: units placed.");
     }
 

All of t$$anonymous$$s works perfect. The problem happens when the server clicks on a "Disconnect" button inside OnGUI() that triggers the following:

         // SERVER DISCONNECT BUTTON
         if (isInitialized)
         {
             if (GUI.Button(new Rect(Screen.width/2, 500, 200, 200), "Disconnect", style))
             {
                 
                 // destroy enemy prefabs on SERVER
                 Destroy(_enemyUnitOne);
                 Destroy(_enemyUnitTwo);
                 Destroy(_enemyUnitThree);
                 Destroy(_enemyUnitFour);
 
                 // trigger destruction of enemy prefabs on CLIENT
                 print ("SERVER: ASKING CLIENT TO DESTROY ITS ENEMY PREFABS...");
                 nV.RPC("DestroyEnemiesInClient", RPCMode.Others);
             }
             
         }


And the DestroyEnemiesInClient function:

 [RPC]
     void DestroyEnemiesInClient()
     {
         print ("CLIENT: DESTROYING ENEMIES...");
         Network.Destroy(_enemyUnitOne);
         Network.Destroy(_enemyUnitTwo);
         Network.Destroy(_enemyUnitThree);
         Network.Destroy(_enemyUnitFour);
         print ("CLIENT: ALL ENEMIES DESTROYED.");
 
         // signal SERVER that it is SAFE to DISCONNECT
         nV.RPC("TriggerDisconnection", RPCMode.Server);    
     }


In t$$anonymous$$s last function I tried first with Destroy(instance) and Network.Destroy(instance) but no luck, the objects never get destroyed yet all prints show that the functions are being called correctly in both server and client.

I'd be grateful if anyone could give me some directions as to what I could be doing wrong :/

Best,

Pzeronow

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 hbalint1 · May 02, 2015 at 08:21 AM

Why don't you Netwrok.Instantiate() the objects with master client? so eveyrone will see them, and don't need to instantiate them locally. Also when someone disconnects, the master server could destroy them with Network.Destroy()

Comment
Add comment · Show 6 · 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 pzeronov · May 03, 2015 at 07:38 AM 0
Share
avatar image hbalint1 · May 03, 2015 at 07:52 AM 0
Share
avatar image pzeronov · May 03, 2015 at 05:06 PM 0
Share
avatar image hbalint1 · May 03, 2015 at 06:18 PM 0
Share
avatar image pzeronov · May 04, 2015 at 05:41 AM 0
Share
Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Get RPC senders ID, or IP? 1 Answer

Networking RPC calls never sent to other clients 1 Answer

network.RPC behavior 1 Answer

client to client ping - NAT punchthrough 1 Answer

RPC Call Mix Up Issues 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