Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Memorix101 · May 18, 2013 at 08:04 PM · gameobjectnetworkrpc

Send GameObject active in network to all clients

Hello, currently I'm trying to send the state of the client/server gameobject to all people on the same server :P

The other players on the server can't see if I switch my PlayerMode. So you start in Hunter mode and when I switch to Hunted the others can't see that. They see my still as Hunter :(

This is my code:

 var PlayerMode : int = 0; // 0 = Hunter, 1 = Hunted
 
 var Hunter : GameObject;
 var Hunted : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
 
 var viewID : NetworkViewID= Network.AllocateViewID();
 
 networkView.RPC("SetPlayerMode", RPCMode.AllBuffered, viewID, PlayerMode);
 
 if(networkView.isMine && Input.GetKey(KeyCode.Alpha2)){
 PlayerMode = 0;
 }
 
 if(networkView.isMine && Input.GetKey(KeyCode.Alpha1)){
 PlayerMode = 1;
 }

}

 @RPC
 function SetPlayerMode (viewID : NetworkViewID, Mode : int) {
  var nView : NetworkView;
    
 if(PlayerMode == 0){
 Hunted.gameObject.active = false;
 Hunter.gameObject.active = true;
 nView = Hunter.GameObject();
 nView.viewID = viewID;
 }
 if(PlayerMode == 1){
 Hunted.gameObject.active = true;
 Hunter.gameObject.active = false;
 nView = Hunted.GameObject();
  nView.viewID = viewID;
 }
     }

I miss something and I don't know how to get this work/go on to get this working. Thank you. Hope you guys can help me :D

Screenshot

Comment
Add comment · Show 3
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 ExTheSea · May 18, 2013 at 08:22 PM 0
Share

So what exactly is your problem? Does the RPC not get called? Or is it the state synchronization which doesn't work?

avatar image Memorix101 · May 18, 2013 at 08:25 PM 0
Share

The other players on the server can't see if I switch my Player$$anonymous$$ode. So you start in Hunter mode and when I switch to Hunted the others can't see that. They see my still as Hunter :(

avatar image Memorix101 · May 18, 2013 at 09:01 PM 0
Share

Hmm. Cool it works, but only when someone will connect spawn and not when they are on the server :(

2 Replies

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

Answer by whydoidoit · May 18, 2013 at 08:40 PM

Ok so there are a two things wrong with your code for networking:

  • You are sending an RPC every Update, that's not likely to work, you should expect to send less frequently and interpolate for positions etc. Surely in this case you just need to send it when it changes?

  • The reason why your code isn't working at all is that you never set the PlayerMode in the RPC call, so none of the clients actually know it!

       @RPC
         function SetPlayerMode (viewID : NetworkViewID, Mode : int) {
         var nView : NetworkView;
         PlayerMode = Mode;
      
         if(PlayerMode == 0){
            Hunted.gameObject.active = false;
            Hunter.gameObject.active = true;
            nView = Hunter.GameObject();
            nView.viewID = viewID;
         }
         if(PlayerMode == 1){
            Hunted.gameObject.active = true;
            Hunter.gameObject.active = false;
            nView = Hunted.GameObject();
            nView.viewID = viewID;
          }
     }
    
Comment
Add comment · Show 3 · 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 whydoidoit · May 18, 2013 at 09:05 PM 0
Share

It's down to the order of your calling in Update as I mentioned - it should be like this:

 function Update () {
  
     var viewID : NetworkViewID= Network.AllocateViewID();
  
     
     if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha2)){
          networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 0);
     }
  
      if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1)){
          networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 1);
      }
 }
avatar image Memorix101 · May 18, 2013 at 09:11 PM 0
Share

Thank ! I already did that ;) Cool its working. Sorry.... It's late here and I'm tired ... (headbang) yes sure it will not work without Player$$anonymous$$ode = $$anonymous$$ode :S Thanks :D

avatar image Bunny83 · May 18, 2013 at 10:20 PM 0
Share

Hmm, i don't think it's wise to call Network.AllocateViewID every Update. Each allocated ViewID has to be unique on all clients. I haven't tested it yet, but i guess Unity will send this information to all clients.

avatar image
0

Answer by Black_Hole_Games · Aug 28, 2017 at 12:25 PM

hey is there a way to do this in c#

Comment
Add comment · Show 2 · 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 Memorix101 · Aug 28, 2017 at 12:55 PM 0
Share

It's the same code. You only need to convert it to C#. But it's for the legacy networking system. You can still use it, but you shouldn't. Here you can find a tutorial series for the new networking system (Unet): https://unity3d.com/learn/tutorials/topics/multiplayer-networking

However, you can also translate it easily to Photon Networking API.

As it seems you don't know how to translate the JavaScript code into C# I guess you're a beginner. Check https://unity3d.com/learn/tutorials to get into program$$anonymous$$g a little better ;)

Well I will translate it for you, but keep in $$anonymous$$d it would be better if you could it yourself and you need to import the correct namespaces otherwise it won't work:

  @RPC
      void SetPlayer$$anonymous$$ode (NetworkViewID viewID, int $$anonymous$$ode) {
      NetworkView nView;
      Player$$anonymous$$ode = $$anonymous$$ode;
   
      if(Player$$anonymous$$ode == 0){
         Hunted.gameObject.SetActive(false);
         Hunter.gameObject.SetActive(true);
         nView = Hunter.GameObject();
         nView.viewID = viewID;
      }
      if(Player$$anonymous$$ode == 1){
         Hunted.gameObject.SetActive(true);
         Hunter.gameObject.SetActive(false);
         nView = Hunted.GameObject();
         nView.viewID = viewID;
       }
  }

  void Update () {
   
      NetworkViewID viewID  = Network.AllocateViewID();
   
      
      if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha2)){
           networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 0);
      }
   
       if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1)){
           networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 1);
       }
  }

avatar image Black_Hole_Games Memorix101 · Dec 30, 2017 at 03:07 AM 1
Share

Thank you for your help!

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

17 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

Related Questions

Problem in Instantiate Gameobject Through Network 0 Answers

Network RPC find sender gameobject 2 Answers

How to get a gameObject having viewID? 1 Answer

Active/Dis-active Gameobject By RPC 1 Answer

Deleteing Objects created by Clients 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