• 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 Ubuntuz · May 19, 2013 at 08:28 PM · multiplayerspawnscritping

Problems with Network.Instantiate

Hello,

I have been following a tutorial for a multiplayer game and have hit a brick wall. I have the following script on a GameObject with the prefabs connected and what not but the game won't spawn my player. There is nothing in the log, no errors, no players in the scene, it just won't work. Script:

 var playerPrefab:GameObject;
 var spawnObject:Transform;
 var gameName:String = "slothworks1";
 
 private var refreshing:boolean;
 private var hostData:HostData[];
 
 private var buttonX:float;
 private var buttonY:float;
 private var buttonW:float;
 private var buttonH:float;
 
 function Start(){
     buttonX = Screen.width * 0.05;
     buttonY = Screen.width * 0.05;
     buttonW = Screen.width * 0.1;
     buttonH = Screen.width * 0.1;
 }
 
 function startServer(){
     Network.InitializeServer(32, 25001, !Network.HavePublicAddress);
     MasterServer.RegisterHost(gameName, "Tutorial Game Name", "This is a tutorial game");
 }
 
 function refreshHostList(){
     MasterServer.RequestHostList(gameName);
     refreshing = true;
 }
 
 function Update(){
     if(refreshing){
         if(MasterServer.PollHostList().Length > 0){
             refreshing = false;
             Debug.Log(MasterServer.PollHostList().Length);
             hostData = MasterServer.PollHostList();
         }
     }
 }
 
 function spawnPlayer(){
     Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
 }
 
 function OnServerInitialized(){
     Debug.Log("Server Initialized");
 }
 
 function OnConnectedToServer(){
     spawnPlayer();
 }
 
 function OnMasterServerEvent(mse:MasterServerEvent){
     if(mse == MasterServerEvent.RegistrationSucceeded){
         Debug.Log("Registered Server");    
     }
 }
 
 function OnGUI(){
     if(!Network.isClient && !Network.isServer){
         if(GUI.Button(Rect(buttonX, buttonY, buttonW, buttonH), "Start Server")){
             Debug.Log("Starting Server");
             startServer();
         }
         
         if(GUI.Button(Rect(buttonX, buttonY * 1.2 + buttonH, buttonW, buttonH), "Refresh Hosts")){
             Debug.Log("Hosts Refreshing");
             refreshHostList();
         }
         
         if(hostData){
             for(var i:int = 0; i<hostData.length; i++){
                 if(GUI.Button(Rect(buttonX * 1.5 + buttonW, buttonY*1.2 + (buttonH * i), buttonW*3, buttonH*0.5), hostData[i].gameName)){
                 Network.Connect(hostData[i]);
                 }
             }
         }    
     }
 }
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 Bunnybomb7670 · May 19, 2013 at 09:46 PM

There may be a few causes of this, you may be making the client call network.Instantiate but then calling something else at the same time, maybe loading a scene which may cause issues, another thing that may be causing it is if you dont have application.runinbackground on, this may be causing issues where the client sends the call to the server, it cant recieve it because its minimized, and then it might not spawn.

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 Ubuntuz · May 20, 2013 at 07:50 PM 0
Share

I ended up solving this shortly after, I need to make the player spawn when the server was started a as well. However, any idea why crouching on my player works when the player is in the scene before starting but not when the player is spawned?

Entering vehicles doesn't work either under the same circumstances.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Set unity process as server and client? 0 Answers

Photon - Loads a level but doesn't spawn player. 1 Answer

Why this spawnpoint script locks my player? 0 Answers

Spawn Player when Room Starts - (Multiplayer) 1 Answer

Networking: Object is instantiated twice instead of once 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