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 Dudledok · Feb 10, 2014 at 01:12 PM · c#networkingmessagemasterserver

How to pass values between networked players?

I have a game set up with three players connected using MasterServer, but now I want to access values from other players, for example, I want each player to be a different character, so I want to limit each player's choice based on other players' choices.

My initial thought was seeing if there is a way to access static variables by doing something like Network.Connections[0].character but there doesn't seem to be a way to do that.

Another idea was to create a game object with a name that matches that character's name whenever a player chooses a character so I could use GameObject.Find to see if that object exists and therefore has been selected, but that seems like a hacky way to do it.

Lastly I've looked into Remote Procedure Calls, but I haven't got my head round that yet. if that's the way to go, I can update this or ask a new question with more details.

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 whydoidoit · Feb 10, 2014 at 01:24 PM

When you create you prefab for the character have it register itself in a list of the current characters.

   using System.Collections.Generic;
   using System.Linq;

   ...


   public static List<Player> players = new List<Player>();

   void Awake() {
        players.Add(this);
   }

   void OnDestroy() {
       players.Remove(this);
   }

Then you need to RPC when a player selects a name

   public string name;

  bool NameSelected(string newName) {
         if(players.Any(p=>p.name == newName)) {
               return false; //Name not available
         }
         networkView.RPC("SetName", RPCMode.All, newName);
         return true;
  }

  [RPC]
  void SetName(string newName) {
       name = newName;
  }
Comment
Add comment · Show 5 · 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 Dudledok · Feb 10, 2014 at 01:51 PM 0
Share

This process is happening in the lobby before the game, so I don't need to create the characters yet. Using your suggested method, creating a list of strings with the characters' names and checking it to see if that player exists should be good, but now I just need to add a string to the list via RPC.

avatar image whydoidoit · Feb 10, 2014 at 01:54 PM 0
Share

Yep that sounds good to me. Just have a single object in the lobby that maintains the list and have it shared between players (an object with a network view that exists in the scene).

avatar image Dudledok · Feb 10, 2014 at 02:00 PM 0
Share

Not sure how to do that, and wouldn't that stop it working when you load a new scene?

avatar image whydoidoit · Feb 10, 2014 at 02:01 PM 0
Share

It's just there to set the names in the lobby right? Just to de-dupe them. I guess you'd give the player the actual name so it wouldn't matter that it wasn't there in the next scene. But it should also work fine if you make it "DontDestroyOnLoad"

avatar image Dudledok · Feb 10, 2014 at 02:04 PM 0
Share

Yeah that's true it doesn't actually matter, I'm just over-thinking it. In your example the RPC updates the local name variable, but I'm not sure why and ins$$anonymous$$d I update the list of players to get it to work. I then also buffer the RPCs to make sure late joiners can't choose selected players.

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

19 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

Related Questions

Port forwarding 1 Answer

Creating servers properly? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Port not opening? 3 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