So I have my two player objects for Unity’s new networking system. I’ve got my Lobby Player and my Game Player objects set up with the LobbyManager. What I can’t figure out is how to find the references to these Player objects within the uNet framework.
I mean, I could go a roundabout way and just have my Player objects register themselves with another of my scripts to keep track of them, but I would really prefer to do it directly through the framework.
So yeah, how do I refer to these Lobby Player and Game Player objects?
You could use 2 arrays, 1 is for NetworkPlayers, another for gameObjects. Fill networkplayers using function OnPlayerConnected, another when it instantiates prefabs. Example of OnPlayerConnected:
var nPArray : Array;
var goArray : Array;
var spawn : Transform;
var yourPrefab : GameObject;
function Start(){
nPArray = new Array(NetworkPlayer);
//if above doesn't work use
//nPArray = new Array();
goArray = new Array(GameObject);
//if above doesn't work use
//goArray = new Array();
}
function OnPlayerConnected(player: NetworkPlayer) {
nPAray.Push(player);
}
Code when player creates a prefab:
function createSomePrefab(){
var go : GameObject = Network.Instantiate(yourPrefab, spawn.position, spawn.rotation, 0);
}
@RPC
function sendID(id : NetworkViewID){
goArray.Push(GetComponent.<NetworkView>().Find(id).gameObject);
}
I am just a bit unsure if it will work correctly when 2 players will connect at almost the same time