• 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 /
  • Help Room /
avatar image
Question by helder227x · Feb 06, 2017 at 08:31 AM · socketsspawn points

Spawn player prefab on sockets c#

i have try to implement the client server to my game the server and client work perfect now i finish my player prefab and i try to spawn the player we is a server and wen is a client i have no erros but then the spawn dont work , i maybe miss something.

there is my code

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
 
 public GameObject Player;
 public float spawnTime = 3f;
 public Transform[] spawnPoints;
 public string IP = "127.0.0.1";
 public int Port = 25001;
 
 public GameObject target;
 
 public string username = "";
 bool RegisterUI = false;
 bool LoginUI = false;
 
 
 
 void OnGUI()
 {
     if(Network.peerType == NetworkPeerType.Disconnected)
     {
         if(GUI.Button(new Rect(100,100,100,25),"Start Client"))
         {
             Network.Connect(IP,Port);
         }
         if(GUI.Button(new Rect(100,125,100,25),"Start Server"))
         {
             Network.InitializeServer(10,Port);
         }
     }
     else {
         if(Network.peerType == NetworkPeerType.Client)
         {
             if(RegisterUI == true && LoginUI == false)
             {
                 username = GUI.TextArea(new Rect(100,125,110,25),username);
 
                 if(GUI.Button(new Rect(100,150,110,25),"Register"))
                 {
                     GetComponent<NetworkView>().RPC("Register",RPCMode.Server,username);
                     RegisterUI = false;
                 }
             }
             else if(LoginUI == true && RegisterUI == false)
             {
                 username = GUI.TextArea(new Rect(100,125,110,25),username);
 
                 if(GUI.Button(new Rect(100,150,110,25),"Login"))
                 {
                     GetComponent<NetworkView>().RPC("Login",RPCMode.Server,username);
                 }
             }
             else {
 
                 GUI.Label(new Rect(100,100,100,25),"Client");
 
                 if(GUI.Button(new Rect(100,125,110,25),"Login"))
                 {
                     LoginUI = true;
                 }
 
                 if(GUI.Button(new Rect(100,150,110,25),"Register"))
                 {
                     RegisterUI = true;
                 }
 
 
                 if(GUI.Button(new Rect(100,175,110,25),"Logout"))
                 {
                     Network.Disconnect(250);    
                 }
             }
 
         }
         if(Network.peerType == NetworkPeerType.Server)
         {
             GUI.Label(new Rect(100,100,100,25),"Server");
             GUI.Label(new Rect(100,125,100,25),"Connections: " + Network.connections.Length);
 
             if(GUI.Button(new Rect(100,150,100,25),"Logout"))
             {
                 Network.Disconnect(250);    
             }
         }
     }
 }
 
 [RPC]
 void Login(string Username)
 {
     if(Network.isServer)
     {
 
         bool checkUsername = PlayerPrefs.HasKey(Username);
 
         if(checkUsername == true)
         {
             GetComponent<NetworkView>().RPC("LoadLevel",RPCMode.Others);
         }   
     }
 }
 
 [RPC]
 void LoadLevel()
 {
     if(Network.isClient)
     {
         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
         //if(Application.loadedLevel == 0)
         //{
             //Application.LoadLevel(0);
         //}
     }
 }
 
 [RPC]
 void Register(string Username)
 {
     if(Network.isServer)
     {
         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
         PlayerPrefs.SetString(Username,Username);
     }
 }
 
 void Spawn ()
 {
     // Find a random index between zero and one less than the number of spawn points.
     int spawnPointIndex = Random.Range (0, spawnPoints.Length);
 
     // Create an instance of the player prefab at the randomly selected spawn point's position and rotation.
     Instantiate (Player, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
 }
 
 
 
 }

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

90 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 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

get receive from socket wifi 0 Answers

Spawn objects around a curved, not circular, path 0 Answers

Unity .NET 4.6 Socket Exception 2 Answers

Android .NET Sockets .dll problem 2 Answers

Some way to use the UWP API within the Unity Editor? 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