- Home /
Send last standing player name
Hello guys :)
I'm currently coding a lastman standing local multiplayer game.
I want to show a text after the match with the player who wins the match, but all I get is the name of the second player prefab (Player 2 is currently called Red) :/
That is my PlayerName Script ....
using UnityEngine;
using System.Collections;
public class OutOfLevel : MonoBehaviour {
public static GameObject GameOverUI;
public static float PlayerActvie = 0f;
public GameObject Explos;
public TextMesh PlayerID;
public string NameID = "Player 1";
public static string SendNameID;
// Use this for initialization
void Start () {
PlayerActvie += 1;
}
// Update is called once per frame
void Update () {
print("my name is " + NameID);
if(!StartEvent.EventGo){
GameObject.Find("UI").SendMessage("SendMsg", NameID);
}
PlayerID.text = "I'm " + NameID;
}
void CheckPlayerActive(){
PlayerActvie -= 1;
Destroy(gameObject);
}
void OnCollisionEnter(Collision collision) {
if(collision.transform.tag == "OutOfLevel"){
CheckPlayerActive();
Instantiate(Explos,transform.position, transform.rotation);
}
}
}
My receiver script.
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static bool Singlepayer = false;
public static bool Multiplayer = false;
public GUIText WinText;
private string WinTextInput = "Nothing received yet";
// Use this for initialization
void Start () {
Singlepayer = false;
Multiplayer = false;
}
// Update is called once per frame
void Update () {
if(StartEvent.EventGo && Multiplayer){
WinText.text = "Victory for " + WinTextInput + " !";
}
// print ("s"+Singlepayer + "m" + Multiplayer + "pa" + OutOfLevel.PlayerActvie);
}
void SendMsg(string meep){
WinTextInput = meep;
}
}
Thank you for your help :)
Answer by Kacheek · May 07, 2014 at 08:28 AM
hmm i would recomand to just go for a tag search... just add the tag "player" to your player prefab and then sync it with an rpc call (i dont see any in your script how do you pass the information over the network?) also you should assign the players name to the players gameobject character just like so :
gameObject.name = playername;
well heres some code you could use :
//create the variables
GameObject[] players;
string lastplayername = "";
string thelastplayer = "";
// check if the variable has been assigned yet if so output the playername
if(thelastplayer != "")
{
print("my name is " + thelastplayer);
debug.log ("my name is " + thelastplayer);
}
//just let the server do this
if(Network.peerType == NetworkPeerType.Server )
{
//create an array with all players that have the tag "player"
players = GameObject.FindGameObjectsWithTag("player");
//if there only is one player
if(players.Length == 1)
{
//save the name of the last player
lastplayername = (players[1]);
//send the RPC to your clients so they know his name
networkView.RPC("telleveryonewhothelastplayeris",RPCMode.All, lastplayername);
}
}
[RPC]
void telleveryonewhothelastplayeris (string thename)
{
// assign the name to the final variable
thelastplayer = thename;
}
Answer by Memorix101 · May 07, 2014 at 11:38 AM
Thank you :) Yes, finally I made it the same :)The weird thing was the first time it didn't work, so I was so frustrated i close Unity. Later today, I reopened it and the code I posted below works instantly O.o Weird thing. THank you for your help :)
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static bool Singlepayer = false;
public static bool Multiplayer = false;
public GameObject player;
public GUIText WinText;
private string WinTextInput = "Nothing received yet";
// Use this for initialization
void Start () {
Singlepayer = false;
Multiplayer = false;
player = null;
StartEvent.EventOver = false;
//player = transform.GetComponent<OutOfLevel>();
}
// Update is called once per frame
void Update () {
print(GameObject.FindWithTag("Player").name);
if(StartEvent.EventOver && Multiplayer){
if(!player){ player = GameObject.FindWithTag("Player");}
WinText.text = "Victory for " + player.GetComponent<OutOfLevel>().NameID + " !";
if(player.GetComponent<OutOfLevel>().NameID == "Red"){
WinText.color = Color.red;
}
if(player.GetComponent<OutOfLevel>().NameID == "Blue"){
WinText.color = Color.blue;
}
}
// print ("s"+Singlepayer + "m" + Multiplayer + "pa" + OutOfLevel.PlayerActvie);
}
}
by the way if noticed that you use GameObject.FindWithTag this will only output one object! not if ther is just one object even if there are 10 players youll get just one of them with the code ive wrote above ittl check if there is just one player its creating an array which will first assign all player names to the array and then check how big that array is i recomand to do that!
if you want a variable to be an array use GameObject[] ins$$anonymous$$d of GameObject ... this [] defines the array!
i hop i could help
Your answer
Follow this Question
Related Questions
Multiplayer - Opposite names above players 1 Answer
How do I scale my prefab over the network? 0 Answers
(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers
In Unity can you make games that have online features and how can you test it to see if it works 2 Answers
Instantiate already connected players. 0 Answers