- Home /
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]);
}
}
}
}
}
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.
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.