How to kick a player
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How to kick a player
Thestranger
2020-01-25 04:25:36
Hi, i just made a script to kick a player from room if player name equal the value and click button he will be kicked out , I'm using Photon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LeaveCurrentMatch : MonoBehaviour
{
public GameObject playerName;
public void OnClick_LeaveMatch()
{
PhotonNetwork.LeaveRoom();
PhotonNetwork.LoadLevel(0);
}
public void Kick(PhotonPlayer kick)
{
InputField inputField = playerName.GetComponent<InputField>();
string value = inputField.text;
PhotonNetwork.playerName = value;
PhotonNetwork.CloseConnection(kick);
}
}
Comments
Thestranger
2020-01-26 00:15:29
Any help please
Hi @Thestranger,
Thank you for choosing Photon!
Example code to get player using its nickname.
Suggestions: assign & cache input field component.
[SerializeField]
private InputField nicknameInputField;
public void Kick()
{
Kick(nicknameInputField);
}
private void Kick(InputField inputField)
{
if (inputField == null)
{
return; // log error?
}
string nickname = inputField.text;
Kick(nickname);
}
private void Kick(string nickname)
{
if (string.IsNullOrEmpty(nickname))
{
return; // log error?
}
foreach(Player player in PhotonNetwork.CurrentRoom.Players)
{
if (!player.IsLocal && nickname.Equals(player.Nickname))
{
Kick(player);
return;
}
}
// log error? player is local or not found?
}
private void Kick(PhotonPlayer playerToKick)
{
if (!PhotonNetwork.IsMasterClient)
{
return; // log error?
}
PhotonNetwork.CloseConnection(playerToKick);
}
Thestranger
2020-02-10 06:45:11
@JohnTube
It worked! Thank you so much, And i want something else, I want when the player kicks he is directed to Lobby
Back to top