- Home /
networkview + GUI
I got a problem with my multiplayer FPS:
The LIFE of each character must be shown to each player.
My problem is that both LIFE variables are showed to each player, then for exemple: If player1 is life=100 and player2 is life=5, what is showed to each player is "LIFE 100" + "LIFE 5", one over another, then it looks like 500 or something like...
I must show each 'life' variable to each player...i think i must use networkview commands but I have no idea how...Can you help me?
parts of my code:
var life=100;
[...start and update functions here...]
@RPC
function ApplyDamage(damage:int) {
if (life<=0) {
life=0;
Network.Destroy(gameObject);
}else {
life-=1;
}
}
function OnGUI() {
GUI.Label(Rect(100,100,100,100),"LIFE "+life);
}
Answer by PrimeDerektive · Jan 06, 2011 at 01:40 PM
Just as an FYI, instead of having to pepper all your scripts randomly with network conditions like "Network.IsServer", "Network.IsClient", and "networkView.IsMine", its useful to do a "network initialization" script for any objects that will be Network.Instantiated.
For example, in your case, you might use a script called "PlayerNetworkInit" that did something like this (for the sake of argument, I'm assuming the name of this health script is PlayerHealth):
PlayerNetworkInit.js:
function OnNetworkInstantiate(info : NetworkMessageInfo){
if(!networkView.IsMine){
GetComponent(FPSWalker).enabled = false;
GetComponent(PlayerHealth).enabled = false;
}
}
This way, you can manage all the code that you want running on remote instances of this object (instances of this object running on clients that don't own it) from one place.
The beautiful thing about RPC's is you can call an RPC on an object even if the script that contains that RPC function is disabled (so in your case, you can disable the PlayerHealth script on all player objects that the current player doesn't own on the network, so it won't show the GUI code, but you can still send the ApplyDamage RPC message on them).
Answer by Henrique · Jan 06, 2011 at 12:20 PM
Sorry...I've Solved!
if (networkView.isMine) {
GUI.Label(Rect(100,100,100,100),"LIFE "+life);
}
Thanks!
Your answer
Follow this Question
Related Questions
How would you set up a Lobby Scene? 1 Answer
network GUI not working 1 Answer
Networking-multplayer issue 1 Answer
Network.Connections 1 Answer