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 Hoang Tuan · Mar 21, 2014 at 04:20 AM · connectionconnectconnecting

void OnConnectedToServer() is not call

Here my server script

 using UnityEngine;
 using System.Collections;
 
 public class CreateServer : MonoBehaviour {
     public string connectToIp = "127.0.0.1";
     public int connectPort = 25001;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void OnGUI(){
         if (Network.peerType == NetworkPeerType.Disconnected) {
             //Ko ket noi , ko la client hay host
             GUILayout.Label ("Connection status : Disconnected");
             //Tao 2 o connect to ip va connect port
             connectToIp = GUILayout.TextField (connectToIp, GUILayout.MinWidth (100));
             connectPort = int.Parse(GUILayout.TextField(connectPort.ToString()));
             //Button connect
             GUILayout.BeginVertical ();
             if (GUILayout.Button ("Start Server")) {
                     //Tao 1 server voi 32 clients su dung port thong qua GUI dien o tren
                     Network.InitializeServer (32, connectPort);
             }
             GUILayout.EndVertical ();
         } else {
             //Neu co connection
             if (Network.peerType == NetworkPeerType.Connecting) {
                 //Thong bao hien thi dang ket noi
                 GUILayout.Label ("Connection status: Connecting");
             }else if (Network.peerType == NetworkPeerType.Server) {
                 //Thong bao hien thi da ket noi
                 GUILayout.Label ("Connection status: Server!");
                 //So luong connection
                 GUILayout.Label ("Connections: " + Network.connections.Length);
                 if (Network.connections.Length >= 1) {
                     //Ping so voi player dau tien
                     GUILayout.Label ("Ping to first player: " + Network.GetAveragePing (Network.connections [0]));
                 }            
             }
             //Tao nut disconnect server
             if (GUILayout.Button ("Disconnect")) {
                 Network.Disconnect (200);
             }
         }
     }
     // NONE of the functions below is of any use in this demo, the code below is only used for demonstration.
     // First ensure you understand the code in the OnGUI() function above.
     
     //Client functions called by Unity
     void OnConnectedToServer() {
         Debug.Log("This CLIENT has connected to a server");    
     }
     
     void OnDisconnectedFromServer(NetworkDisconnection info) {
         Debug.Log("This SERVER OR CLIENT has disconnected from a server");
     }
     
     void OnFailedToConnect(NetworkConnectionError error){
         Debug.Log("Could not connect to server: "+ error.ToString());
     }
     
     
     //Server functions called by Unity
     void OnPlayerConnected(NetworkPlayer player) {
         Debug.Log("Player connected from: " + player.ipAddress +":" + player.port);
     }
     
     void OnServerInitialized() {
         Debug.Log("Server initialized and ready");
     }
     
     void OnPlayerDisconnected(NetworkPlayer player) {
         Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port);
     }
     
     
     // OTHERS:
     // To have a full overview of all network functions called by unity
     // the next four have been added here too, but they can be ignored for now
     
     void OnFailedToConnectToMasterServer(NetworkConnectionError info){
         Debug.Log("Could not connect to master server: "+ info);
     }
     
     void OnNetworkInstantiate (NetworkMessageInfo info) {
         Debug.Log("New object instantiated by " + info.sender);
     }
     
     void OnSerializeNetworkView(BitStream stream,NetworkMessageInfo info)
     {
         //Custom code here (your code!)
     }
     
     /* 
      The last networking functions that unity calls are the RPC functions.
      As we've added "OnSerializeNetworkView", you can't forget the RPC functions 
      that unity calls..however; those are up to you to implement.
      
      @RPC
      function MyRPCKillMessage(){
         //Looks like I have been killed!
         //Someone send an RPC resulting in this function call
      }
     */
 }

And here is my clients script

 using UnityEngine;
 using System.Collections;
 
 public class ConnectToServer : MonoBehaviour {
     public string connectToIP = "127.0.0.1";
     public int connectPort = 25001;
 
     // Use this for initialization
     void Start () {
         //Application.runInBackground = true;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnGUI () {
         if (Network.peerType == NetworkPeerType.Disconnected) {
             //We are currently disconnected: Not a client or host
             GUILayout.Label ("Connection status: Disconnected");
 
             connectToIP = GUILayout.TextField (connectToIP, GUILayout.MinWidth (100));
             connectPort = int.Parse(GUILayout.TextField (connectPort.ToString ()));
             
             GUILayout.BeginVertical ();
             if (GUILayout.Button ("Connect as client")) {
                 //Connect to the "connectToIP" and "connectPort" as entered via the GUI
                 //Ignore the NAT for now
                 Network.Connect (connectToIP, connectPort);
             }
             GUILayout.EndVertical ();
         } else {
             //We've got a connection(s)!
             if (Network.peerType == NetworkPeerType.Connecting) {
                 GUILayout.Label ("Connection status: Connecting");
             } else if (Network.peerType == NetworkPeerType.Client) {
                 GUILayout.Label ("Connection status: Client!");
                 GUILayout.Label ("Ping to server: " + Network.GetAveragePing (Network.connections [0]));
 
                 Application.LoadLevel("BeginBattle");
             }            
             if (GUILayout.Button ("Disconnect")) {
                 Network.Disconnect (200);
             }
         }
     }
 }

And here is my spawn player scripts

 using UnityEngine;
 using System.Collections;
 
 public class SpawnPlayer : MonoBehaviour {
     public Transform playerPrefab ;
 
     // Use this for initialization
     void Start () {
         //Spawnplayer();
         //Application.runInBackground = true;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnConnectedToServer (){
         Debug.Log ("OnConnectedToServer");
         Spawnplayer();
     }
 
     void OnServerInitialized (){
         Debug.Log ("OnServerInitialized");
         Spawnplayer();
     }
 
     void Spawnplayer(){
         //Spawn khi co mang
         Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
         //Spawn khi ko co mang
         //Instantiate (playerPrefab, transform.position, transform.rotation);
     }
 
     void OnPlayerDisconnected(NetworkPlayer player) {
         Debug.Log("Clean up after player " + player);
         Network.RemoveRPCs(player);
         Network.DestroyPlayerObjects(player);
     }
     
     void OnDisconnectedFromServer(NetworkDisconnection info) {
         Debug.Log("Clean up a bit after server quit");
         Network.RemoveRPCs(Network.player);
         Network.DestroyPlayerObjects(Network.player);
         
         /* 
     * Note that we only remove our own objects, but we cannot remove the other players 
     * objects since we don't know what they are; we didn't keep track of them. 
     * In a game you would usually reload the level or load the main menu level anyway ;).
     * 
     * In fact, we could use "Application.LoadLevel(Application.loadedLevel);" here instead to reset the scene.
     */
         Application.LoadLevel(Application.loadedLevel);
     }
 }
 

My problem is my void OnConnectedToServer() in SpawnPlayer.cs is not running , i think i was connect success because line

 Application.LoadLevel("BeginBattle");

pass , and i connect to the server , then why my void OnConnectedToServer() not running , pls help :D

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

Answer by Robert Carlsson · Apr 29, 2014 at 06:47 AM

From what I can see you're spawning the player then changing level, so unless you use don't destroy on load on the player it won't be there when your scene loads.

OnConnectedToServer is run as fast as you connect, then the GUI Application.LoadLevel gets called since you have a connection. Therefore the gameobject you're spawning is erased when the next scene is loaded.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

c# not going to destination on 0 hp 1 Answer

Footstep Script Not Working 2 Answers

Script error help! 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