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 Jason Jolley · Apr 03, 2011 at 05:42 AM · c#multiplayerserverclient

How do I connect a client to a server?

I have this simple c# script that is attached to my main camera object.(Included below) I run two different instances, one as a server and the other as a client.

I start the server first. The server Network.peerType changes properly. Then I start the client. I type in the proper ipaddress and port and hit connect. There are no network errors, but the Network.peerType never changes on the client and the OnConnectedToServer function never gets called. However everytime I hit the Connect button, the OnPlayerConnect on the server gets called. So there's some type of client server communication. It just seems like the client is missing something.

I'm stuck and I really appreciate the help!

Thanks :)

//Code Start

using UnityEngine; using System.Collections;

public class ConnectionGUI : MonoBehaviour {

private string remoteIp = "127.0.0.1"; private int remotePort = 25000; private int listenPort = 25000; private bool useNAT = false; private string yourIP = ""; private string yourPort = "";

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

}

void OnGUI(){ if(Network.peerType == NetworkPeerType.Disconnected){ //Not connected

     if(GUI.Button(new Rect(10,10,100,30),"Connect"))
     {
         //Connecting to the server
         Debug.Log(remoteIp);
         Debug.Log(remotePort);
         NetworkConnectionError ne = Network.Connect(remoteIp,remotePort);
         Debug.Log(ne.ToString());
         Debug.Log(Network.peerType);
     }
     if(GUI.Button(new Rect(10,50,100,30),"Start Server")){
         //Creating server
         Network.InitializeServer(32,listenPort, useNAT);
         Object[] gameObjects = FindObjectsOfType(typeof(GameObject));
         foreach(GameObject go in gameObjects){
             go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
         }
     }

     remoteIp = GUI.TextField(new Rect(120,10,100,20),remoteIp);
     remotePort = int.Parse(GUI.TextField(new Rect(230,10,40,20),remotePort.ToString()));
 }else{
     //Getting your ip address and port
     string ipaddress = Network.player.ipAddress;
     string port = Network.player.port.ToString();

     GUI.Label(new Rect(140,20,250,40),"IP Address:"+ipaddress+":"+port);
     if(GUI.Button(new Rect(10,10,100,50),"Disconnect"))
     {
         //Disconnect from the server
         Network.Disconnect(200);
     }
 }

}

void OnConnectedToServer(){ Debug.Log("Connected to Server"); Object[] gameObjects = FindObjectsOfType(typeof(GameObject));

 foreach(GameObject go in gameObjects){
     go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
 }

}

void OnPlayerConnected(){ Debug.Log("Player connected"); }

}

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
5

Answer by Jason Jolley · Apr 03, 2011 at 06:02 AM

I figured it out. I needed to make sure that I went to Edit -> Project Settings -> Player. Then under Per Platform Settings -> Resolution and Presentation -> Resolution. Then check Run in Background.

Hope this helps somebody :)

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 kynous · May 10, 2012 at 07:39 PM

That helped me, thanks.

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 moghes · Apr 15, 2013 at 04:45 PM 0
Share

Is this an answer?? then why is the comments used for?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiplayer server-client setup 1 Answer

Unity networking tutorial? 6 Answers

Multiplayer push object error. 0 Answers

Help with multiplayer connection 0 Answers

Multiplayer syncronisation 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