- Home /
player spawning
Hello! I need help spawning my player through unet!
I'm using the MatchMaker script provided from the documentation here (Except I'm making it derive from NetworkManager): http://docs.unity3d.com/Manual/UNetMatchMaker.html
And I added my own custom code to spawn the player after the client has connected. The problem I'm facing is that the player will not spawn no matter what. I'm not getting any error messages either -.. It's just not spawning. Why?
Here is my spawn code --
public void OnConnected(NetworkMessage msg)
{
ClientScene.AddPlayer(myClient.connection, 0);
Debug.Log("Connected!");
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
Debug.Log("spawn player");
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
Any help is greatly appreciated and will get you guys cookies! :)
I don't know, but shouldn't the
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
line be the first line in OnServerAddPlayer(...)
?
And do you get the Log "spawn player"?
No, because you must instantiate the object in the server before adding it for the clients.
And oddly, no. I'm not getting the debug message so it's as if the OnServerAddPlayer isn't even being called.
This is my code for spawning random player. maybe you'll find what you've fogot or missed.
Also, make sure to put your player in your spawn info....
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;
using System.Collections;
public class PlayerPrefmange : Network$$anonymous$$anager {
public GameObject playerPrefab1;
public GameObject playerPrefab2;
public GameObject playerPrefab3;
public GameObject playerPrefab4;
int randomNumber ;
Vector3 playerSpawnPos= new Vector3(1,1,1);
// Use this for initialization
void Strat () {
randomNumber=Random.Range(0,5);
}
// Update is called once per frame
void Update () {
randomNumber=Random.Range(0,5);
}
public override void OnServerReady(NetworkConnection conn) {
NetworkServer.SetClientReady(conn);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
if (randomNumber == 0) {
Debug.Log ("playerPrefab");
playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
}
if (randomNumber == 1) {
Debug.Log ("playerPrefab1");
playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab1, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
}
if (randomNumber == 2) {
Debug.Log ("playerPrefab2");
playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab2, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
}
if (randomNumber == 3) {
Debug.Log ("playerPrefab3");
playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab3, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
}
if (randomNumber == 4) {
Debug.Log ("playerPrefab4");
playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab4, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
}
}
}