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
0
Question by artberry · Sep 08, 2015 at 09:28 AM · networkingplayerserverinitialization

UNET. Proper way to set player's team OnServerAddPlayer.

Hi. Help me please with this question. How can I set any initial variables for added player (client) at connection moment?

For example, I need to set player's team, immediately after it connects to the game. I am trying to implement this approach from the docs: http://docs.unity3d.com/Manual/UNetPlayers.html

 class MyManager : NetworkManager
 {
     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
     {
         GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.Zero, Quaternion.Identity);
         player.GetComponent<Player>().color = Color.Red;
         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
     }
 }

It works fine, player prefab got red color, but on the host side only. Any clients which connect to the game do not see any changes in player's prefab.

There is also said: "A custom implementation of OnServerAddPlayer must also call NetworkServer.AddPlayer...", but is seems that there is no such a method (AddPlayer) in the NetworkServer class http://docs.unity3d.com/ScriptReference/Networking.NetworkServer.html

So, could you help please and tell what is the right way to set any initial variables for connected players?

Thanks in advice. Sorry for weak English.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Yanda · Dec 03, 2015 at 12:43 PM

Hello, The thing is that the function NetworkServer.AddPlayer does not have to be called from within OnServerAddPlayer. As long as the correct connection object and playerControllerId are passed in, it can be called after OnServerAddPlayer has returned. This allows asynchronous steps to happen in between, such as loading player data from a remote data source.

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 Alturis2 · Sep 27, 2016 at 02:06 AM 0
Share

Yanda: What is the key to getting OnServerAddPlayer() to be called at all?

avatar image
0

Answer by Zefalcon · Sep 06, 2017 at 05:30 PM

@artberry

Not sure if you still need an answer to this or not, but I was having similar problems with my program, where pressing the space bar was supposed to change the color of the player object.

To solve this, I had to use the [Command] and [ClientRpc] tags and have two separate methods doing the coloring. Here's a snippet of my code. Hopefully it helps.

 void Update(){
 
    if (!isLocalPlayer){
       return;
    }
 
    if (Input.GetKeyDown(KeyCode.Space)){
       CmdColorChange(transform.gameObject, currentColor);
    }
 }
 
 [Command]
 void CmdColorChange(GameObject obj, Color toChange){
    RpcColorChange(obj, toChange);
 }
 
 [ClientRpc]
 void RpcColorChange(GameObject obj, Color toChange){
    obj.GetComponent<MeshRenderer>().material.color = toChange;
 }
 
 

Basically, the [Command] tag means that method is only called on the server, while [ClientRpc] means the method is only called on the clients. For whatever reason, changing the color just on the server (which I imagine is where OnServerAddPlayer() is called) doesn't change it on any of the clients, so the server has to then tell the clients they also need to change the color, via a [ClientRpc] method.

One note: if you use a [Command] or [ClientRpc] tag, the method names have to have either Cmd or Rpc at the beginning, or they won't work.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

How to check when a new client/local player is loaded? 1 Answer

Multiplayer: Only see Players moving on Host 0 Answers

[Multiplayer]How to individualize the Player 1 Answer

Instantiated object from client player not moving when spawned. How can I fix this? 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