• 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 Guardian-Angel · Jun 14, 2012 at 11:44 AM · networkserverclientconnectiondisconnect

Client connects to server, but Network.peerType returns Disconnected until next click

Hey all :)

I'm kinda new with all this Server-Client programming, so I followed some tutorials which gave me a basic idea of how to make it work, but now I have this really annoying problem

When I click a login button, it should run my Network.Connect("127.0.0.1", 2602); After this I should be online with my client, which is true, because I check for connections on my Server which tells me that I have one signed on, but the wierd thing is that when I print(Network.peerType); I'm given Disconnected, even though my Server says I'm connected. Next time I click the login button, it tells me I'm Client

Might it be because of my code that takes too much time to run?

function OnGUI(){

 if(!loggedIn){
 
     if(!registering){
     
         GUI.BeginGroup(new Rect(Screen.width/2 - 300, Screen.height/2 - 300, 600, 600));
         GUI.Box(new Rect(0,0,600,600),"Gladiator Wars");
         
         username = GUI.TextArea(new Rect(200,50,200,25),username);
         password = GUI.TextArea(new Rect(200,75,200,25),password);
         
         if(GUI.Button(new Rect(200,100,200,25), "Login")){
             
             Connect();
             
             print(Network.peerType);
             
         }
 
     }
 

}

}

function Connect(){

 if(Network.peerType == NetworkPeerType.Disconnected){
     
     Network.Connect("127.0.0.1", 2602);
 
 }
 
 if(Network.peerType == NetworkPeerType.Client){
 
     networkView.RPC("Login", RPCMode.Server, username, password, Network.player);
 
 }
 
 print(Network.peerType);

}

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 mark03468 · Jul 03, 2012 at 05:49 PM

Network.Connect() needs some time to made connection.

Try to use :

 // For Server
     function OnPlayerConnected (player : NetworkPlayer) 
     {
         networkView.RPC("RequestLoginInfo" , player , true);
     }

 // For Client
     @RPC
     function RequestLoginInfo (loggedIn : boolean)
     {
        networkView.RPC("Login", RPCMode.Server, username, password, Network.player);
     }

or use function OnConnectedToServer () in client

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
0

Answer by gilmarhz · Oct 19, 2012 at 07:52 PM

Sorry, it's late to answer this, but google shows this link in first place when I search for "Network.peerType". For those people who came here looking for answer about this problem, it isn't a unity bug, just set "Run In Background = true" at Player Settings and "Network.peerType ==NetworkPeerType.Disconnected" is work fine.

Para quem está lendo em português, se não setar "Run In Backgroud = true" em Edit/Project Settings/Player e fizer testes locais em sua máquina conectando duas instâncias do jogo, a que tem o server estará em background quando o cliente conectar, e Network.peerType continuará == NetworkPeerType.Disconnected e os exemplos encontrados na net não funcionarão.

Espero ter ajudado.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Can anyone help me with my network script? 1 Answer

How connect client to server when ip is private ? 2 Answers

Server initialization not accepting connections. 0 Answers

Is there a way to notify to the app once client has connected to server? 2 Answers

How do i control An Object between Server and Client 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