- Home /
NetWork: Random Player
HI, I try to spawn a different playerprefab each time the client connect the server. So, basicly, I try this just to teste if the prefab have changed befor script the random part. ex:
using UnityEngine. using UnityEngine.Networking; public class PlayerPrefManager : NetworkManager {
public GameObject playerPrefab;
public GameObject playerPrefab2;
public override void
OnServerReady(NetworkConnection conn) {
playerPrefab=playerPrefab2;
}
}
But nothing happen. How can I rosolve this whitout change anything to my playerprefab because it is done.
Answer by TheSorm · Apr 28, 2016 at 10:24 AM
You can just override this methode:
// called when a new player is added for a client
public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
And put your different players randomly.
Thanks for your answer, but it's not usefull. Where did you take your "playerSpawnPos". Where the "var player'' comme from? You script in C-sharp and changed to Java and reScript in C-Sharp ?? So' I'll put my code and maybe it will make more sens to you. :)
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerPrefmange : Network$$anonymous$$anager {
public GameObject playerPrefab;
public GameObject playerPrefab2;
public GameObject playerPrefab3;
public GameObject playerPrefab4;
int randomNumber ;
// Use this for initialization
void Strat () {
randomNumber=Random.Range(0,4);
}
// Update is called once per frame
public override void OnServerReady(NetworkConnection conn)
{
if(randomNumber==0){
Debug.Log ("playerPrefab");
playerPrefab=playerPrefab;
}
if(randomNumber==1){
Debug.Log ("playerPrefab1");
playerPrefab=playerPrefab1;
}
if(randomNumber==2){
Debug.Log ("playerPrefab2");
playerPrefab=playerPrefab2;
}
if(randomNumber==3){
Debug.Log ("playerPrefab3");
playerPrefab=playerPrefab3;
}
if(randomNumber==4){
Debug.Log ("playerPrefab4");
playerPrefab=playerPrefab4;
}
}
}
So bassicly it's what I try to do. Change the playerPrefab to my playerPrefab(number).
Thanks for your help .
no this is the official OnServerAddPlayer function from Unity, this is the funktion you have to override (Same like you do with OnServerReady) and put your player prefab as "playerPrefab".
Okey!!! I don't want to be rude because the english it's not my first language but...Did you try your code before send your answer?? I put a Debug.Log("Good");
in your line of code and nothing happen. Also, some error come from your Instatiate transform.position. Of course I changed it for Vector3.zero but...I stay with the fact that the Debug.Log(""); Show me nothing in the console. So basacly, can you please, tell me what is your varible player??? From where did you get this variable?? Did I need to change var player for playerPrefab??? Where I put my variable playerPrefab2 , playerPrefab3, playerPrefab4.....etc?? And you use Instantiate and why not network.Instantiate?? Please, if your not abble to answer correctly, don't post anymore. I want to learn and not to be confuse and turning arround it too long . I need to progress... I anderstand that you try to help and tell me that is in the OnServerAddPlayer
function then i need to put my code...but where and how?????? It's too longue time I try and try and try. Almost a week. So, I'm not asking for nothing because I tryed a lot code and no result. It's for this reason then I come here. And also I'm not the only one who is trying and asking for that easy answer. like I said, I don't want to be rude. It's not a forum. But when I try something that some one posted and I have error on debug or not ennought concreat explication. It's p*** me.....
Thanks again,
Sorry but if you don't understand my anser try out unity tutorials on the tutorial page . Unity Network is advance work and you have to know about override methods and accesing the right scripts. I cut post you a functional script but I don't think you would learn how to do it just give you the functional code
You talk about override, but you use virtual. And if I'll use override. It make and bigggg error on the console. And like I said, the debug.log show nothing when the host is connect or the client. Amd yessssss I will learn whith the entire code because I'll use it for other thing and not be stuck. If people don't show how or explain how to do it or in details like . Ex : Ho, this is for this, that it's for that. You should do this for that, and that for this. The only way I figure out How to do this it's like this.... I put my different player in an Empty gameObject and in the Start() I'll say if randomNumber it's 1, close the player 2,3,4 and detatch me from my parent. But if I"ll try that way, I'll will need to restart my entire game. And I'm asking a way without change enything on my fisnished game.
public class PlayerPrefmange : Network$$anonymous$$anager {
public GameObject playerPrefab1;
public GameObject playerPrefab2;
public GameObject playerPrefab3;
public GameObject playerPrefab4;
Vector3 playerSpawnPos = new Vector3(1,1,1);
int randomNumber;
// Use this for initialization
void Strat() {
randomNumber = Random.Range(0, 4);
}
public override void OnServerReady(NetworkConnection conn) {
if (randomNumber == 0) {
Debug.Log("playerPrefab");
playerPrefab = playerPrefab;
}
if (randomNumber == 1) {
Debug.Log("playerPrefab1");
playerPrefab = playerPrefab1;
}
if (randomNumber == 2) {
Debug.Log("playerPrefab2");
playerPrefab = playerPrefab2;
}
if (randomNumber == 3) {
Debug.Log("playerPrefab3");
playerPrefab = playerPrefab3;
}
if (randomNumber == 4) {
Debug.Log("playerPrefab4");
playerPrefab = playerPrefab4;
}
NetworkServer.SetClientReady(conn);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
GameObject player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
You override the OnServerReady function and you have to call " NetworkServer.SetClientReady(conn);" than you override "OnServerAddPlayer" so you can set what player prefab is loadet for the player spawning.
Soory i cant test this but i hope you understand whats up in this code if not just look at http://docs.unity3d.com/$$anonymous$$anual/UNet$$anonymous$$anager.html and learn how to edit the Network player functions.
Thanks a lot!!!! Thats my boy!!! It doesn't work on a first try but after play around it. I found the solution easly. So basicly, you gived to me the right call thath I missed. NetworkServer.SetClientReady(conn);
Also I change the player variable for playerPrfab and change the playerPrefab in the Instantiate for my custom Player1,2,3,4etc... So with this I made my script that way..... :)
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;
public class PlayerPrefmange : Network$$anonymous$$anager {
public GameObject playerPrefab;
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
public List<GameObject> spawnPrefabs;
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(playerPrefab1, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId); }
if (randomNumber == 1) {
Debug.Log("playerPrefab1");
playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab1, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId); }
if (randomNumber == 2) {
Debug.Log("playerPrefab2");
playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab2, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId); }
if (randomNumber == 3) {
Debug.Log("playerPrefab3");
playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab3, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId); }
if (randomNumber == 4) {
Debug.Log("playerPrefab4");
playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab4, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId); }
}
}
So, everytime when some one is connected, the playerPrefab change for any player then I made. Now I need to researsh for the net work StartPosition. :P So sorry to was maybe rude...but now I leraned a lot and can work forward to use this methode for otherthings. :) 2 thumbs up......!!!!
Hummm... the random player work great but it give me an other big problem. Now, just the host can see all player and effect. I think it's because it's not a Network.Intantiate. but, if I try playerPrefab = (GameObject)Network.Instantiate(playerPrefab3, Vector3.zero, Quaternion.identity,0);
it tell me
Failed Network.Instantiate because we are not connected.
UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
PlayerPrefmange:OnServerAddPlayer(NetworkConnection, Int16) (at Assets/PlayerPrefmange.cs:54)
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
So I dont know what to do but I'm close...I know!
Hooooo, I found my error!!!! I didn't put the other custom player in the SpawnInfo. it was for that reason only the host was able to see other player. So we don't need to call Network.Instanciate. Thanls you so much. I'll be able to help other french people... Again, 2 Thumbs Up!!
Answer by MakakWasTaken · Apr 30, 2016 at 07:32 PM
Can't you make an array of GameObjects and then use Random.range(0,array.Length) to select the index and then use the override that @TheSorm posted to spawn that specific prefab