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
1
Question by easygaming · Feb 05, 2016 at 05:03 AM · networkspawninghud

OnServerAddPlayer() is not called

I added NetworkManager and add my player (PlayerCube) on the NetworkManager.

Although the server and client connected well with the HUD, the OnServerAddPlayer() function is not called.

I found it is not called in Client side, but it is not called Server side, either. (There's no change on GUI, to display 'tmp')

The class MyNetManagerScript is my custom script for the NetworkManager, as shown below.

Can anyone advise me how to solve this problem?

alt text

 public class MyNetManagerScript : NetworkManager {
 
     string tmp = " ";
 
     void OnGUI()
     {
         // ME HERE; is printed on the screen of course, but...
         GUILayout.Label("ME HERE; " + tmp);
     }
 
 
     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
     {  // IS THIS CALLED? IF NOT, WHY NETWORK PLAYER CAN BE MOVED BY KEY?
         Debug.Log("A Player Connected"); // NOT PRINTED in the console
         tmp = "ON PLAYER";  // NOT SHOWN on the screen
 
         OnServerAddPlayer(conn, playerControllerId, extraMessageReader);
     }
 }


screenshot.jpg (76.9 kB)
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 Alturis2 · Sep 27, 2016 at 02:03 AM 0
Share

I am having this exact same problem and would love to hear the answer as well.

I find the Unity documentation extremely lacking in details for many things like this.

If there are limitations or requirements to how to envoke OnServerAddPlayer() it is certainly not being explained in the docs.

avatar image rhadamanthus · Oct 10, 2016 at 06:31 AM 0
Share

I have the same problem as well. There is an issue in the issue tracker that says the method isn't called when auto create players is on, but the OnServerAddPlayer is not called for me even if I disable that option and call ClientScene.AddPlayer... I don't know what to do...

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by andrewzimmer · Dec 02, 2016 at 06:30 PM

I was struggling with this same problem and eventually fixed it by changing the method description. There are 2 versions of OnServerAddPlayer

This one

 public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)

And this one

 public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId);

For whatever reason, the second one with only two variables is what worked for me.

For reference, I also UNCHECKED the Auto Create Players option on the Network Manager, and added the OnClientConnect call.

Here's my function overrides in a custom subclass of Network Manager

 public override void OnClientConnect(NetworkConnection conn) {
         ClientScene.AddPlayer(conn, 0);
     }
 
     public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId) {
         print ("Player Added to Server Yo");
         base.OnServerAddPlayer (conn, playerControllerId);
     }

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 Albertkaruna · Apr 13, 2017 at 09:38 AM 0
Share

Thaks man. it works nice.

avatar image Kerozard · Aug 29, 2017 at 02:58 PM 0
Share

Had the same issue in V 2017.1.0p4 and using the two-parameter overload made it work.

Thank you!

avatar image
1

Answer by markgrossnickle · Dec 14, 2017 at 07:27 PM

I ran across a similar issue in that my OnServerAddPlayer was never called. It turns out it was because I added my own custom handler on the client for MsgType.Connect. I didn't realize RegisterHandler replaced other handlers and I assumed incorrectly it just added its own.

Leaving this here in case someone else stumbles on the same assumption.

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
avatar image
-3

Answer by 11Buff · Oct 10, 2016 at 08:39 AM

Just a tip, not needed but it's a habit. Don't do:

string tmp = " ";

Instead

string tmp;

This is my preference. Just saying. Maybe you need it to be " ";

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 malkere · Nov 20, 2018 at 03:50 AM 1
Share

string tmp; will create an uninitialized variable resulting in a null reference error should anything attempt to use it. By adding the = ""; it becomes an empty string, or in the above case a single space that can be used. They are quite different things. If you are certain you will populate the string before ever using it then you don't need to initialize it, but in his case he's using it on OnGUI, so the code would not work if he did not initialize it.

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

45 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

Related Questions

How to find gameobject through SpawnMessage? 0 Answers

IS it possible to have delayed player spawning after server connection? (UNet) 1 Answer

NetworkManagerHud overlapping on build? 2 Answers

[UNet] How to control spawned object on client side? 2 Answers

i'm trying to spawn enemies and it goes well but when i'm trying to scale them it doesn't sync i used [ClientRpc] and it didn't work 1 Answer


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