Send last standing player name

Hello guys :slight_smile:

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) :confused:

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 :slight_smile:

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;
}

Thank you :slight_smile: 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 :slight_smile:

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);

	}
	
}