Hello i want know how i cant send my username to other scene with RPC and get in the other Scene.?
Hi SROZOne, you can do as:
string username;
[RPC]
void SetUsername(string usernameReceived)
{
this.username = usernameReceived;
}
Call this by doing:
networkView.RPC("SetUsername", RPCMode.Others, "usernameToSendToOtherPlayers");
Depending on what you plan on doing with the username, this will be sufficient so long as there are only two clients or server and client depending on how you’re doing things.
I’d suggest reading:
Hope this helps.
- Ben
can you check how i can add in this script?
using UnityEngine;
using System.Collections;
public class Connection : MonoBehaviour
{
public string username = "";
public string password= "";
public string repassword= "";
public string email= "";
public string message= "";
public GUISkin hdskin;
public int HofBox = 25;
public string loguser= "";
private string logpass= "";
private string logmessage= "";
private string mainDomainName= "http://avct.sytes.net/mmorpg";
private int generatednumber = 0;
private int numberinput;
private bool islogin = true;
private bool isregister = false;
private string CurrentMenu = "Menu";
void OnGUI ()
{
if(CurrentMenu == "Menu")
{
GUI.skin = hdskin;
if(islogin == false && isregister == true)
{
GUI.Box( new Rect(Screen.width /2-250,20,500,480),"");
GUI.Label( new Rect(Screen.width /2-100,30,200,50),"Please fill out the fields");
GUI.Label( new Rect(Screen.width /2-230, 130,200,50),"Username:");
username = GUI.TextField( new Rect(Screen.width /2-20,130,200,HofBox),username,45);
GUI.Label( new Rect(Screen.width /2-230,190,200,50),"Password:");
password = GUI.PasswordField( new Rect(Screen.width /2-20,190,200,HofBox),password,"*"[0],45);
GUI.Label( new Rect(Screen.width /2-230,250,200,50),"Re-Enter pass:");
repassword = GUI.PasswordField( new Rect(Screen.width /2-20,250,200,HofBox),repassword,"*"[0],45);
GUI.Label( new Rect(Screen.width /2-230,310,200,50),"Email:");
email = GUI.TextField( new Rect(Screen.width /2-20,310,200,HofBox),email,45);
GUI.Label( new Rect(Screen.width /2-230,360,150,50),"Please verify:");
GUI.Label( new Rect(Screen.width /2-80,360,100,HofBox),generatednumber.ToString());
numberinput = int.Parse(GUI.TextField( new Rect(Screen.width /2+80,360,100,HofBox),numberinput.ToString()));
if(GUI.Button( new Rect(Screen.width /2-230,420,200,HofBox),"Register!"))
{
message = "";
if(username == "" || email == "" || password == "" || numberinput == 0){
message += "Please fill in the empty fields. \n";
}else{
if(password == repassword){
if(numberinput == generatednumber){
StartCoroutine("doRegister");
}else{
message += "The number you inputed is not the same number.";
}
}else{
message += "The passwords do not match. \n";
}
}
}
if(GUI.Button( new Rect(Screen.width /2-20,420,200,HofBox),"Return to login"))
{
islogin = true;
isregister = false;
username = ""; //cleaning our variables
password = "";
repassword = "";
email = "";
numberinput = 0;
message = "";
loguser = "";
logpass = "";
logmessage = "";
}
if(message != "")
{
GUI.Label( new Rect(Screen.width /2-230,460,460,50), message);
}}
else if(islogin == true && isregister == false)
{
StartCoroutine (ServerRefresh ());
GUI.Box( new Rect(Screen.width /2-250,20,500,450),"");
if(MasterServer.PollHostList().Length != 0)
{
HostData[] data = MasterServer.PollHostList();
foreach(HostData c in data)
{
GUILayout.BeginArea(new Rect(Screen.width /2-65,30,200,50));
GUILayout.Label(c.gameName + " " + (c.connectedPlayers - 1) + "/" + c.playerLimit);
GUILayout.EndArea();
}
}
else
{
GUILayout.BeginArea(new Rect(Screen.width /2-65,30,200,50));
GUILayout.Label("Server Offline");
GUILayout.EndArea();
}
GUI.Label( new Rect(Screen.width /2-230, 130,200,50),"Username:");
loguser = GUI.TextField( new Rect(Screen.width /2-20,130,200,HofBox),loguser,45);
GUI.Label( new Rect(Screen.width /2-230,230,200,50),"Password:");
logpass = GUI.PasswordField( new Rect(Screen.width /2-20,230,200,HofBox),logpass,"*"[0],45);
if(GUI.Button( new Rect(Screen.width /2-230,330,200,HofBox),"Login")){
if(loguser == "" || logpass == ""){
logmessage += "Please enter the necessary data! \n";
} else{
logmessage = "";
StartCoroutine("doLogin");
}
}
if(GUI.Button( new Rect(Screen.width /2-20,330,200,HofBox),"Register"))
{
islogin = false;
isregister = true;
generatednumber=Random.Range(1000,99999);
username = ""; //cleaning our variables
password = "";
repassword = "";
email = "";
numberinput = 0;
message = "";
loguser = "";
logpass = "";
logmessage = "";
}
if(logmessage != "")
{
GUI.Label( new Rect(Screen.width /2-230,360,460,50),logmessage);
}
}
}//CurrentMenu = Menu
}//OnGui
public void OnConnectedToServer()
{
islogin = false;
isregister = false;
PlayerPrefs.SetString("Player Name", loguser);
Application.LoadLevel (2);
}
private void OnDisconnectedFromServer()
{
PlayerPrefs.DeleteKey ("Player Name");
CurrentMenu = "Menu";
}
private IEnumerator doRegister()
{
WWWForm form = new WWWForm();
form.AddField("Username" , username);
form.AddField("Password" , password);
form.AddField("Email" , email);
var w = new WWW(mainDomainName + "/Register.php", form);
yield return w;
if(w.error == null){
message += w.text;
StopCoroutine("doRegister");
}else{
message += "Error : " + w.error + "\n";
}
}
private IEnumerator doLogin()
{
WWWForm logform = new WWWForm();
logform.AddField("Username" , loguser);
logform.AddField("Password" , logpass);
var logw = new WWW(mainDomainName + "/Login.php", logform);
yield return logw;
if(logw.error == null){
logmessage += logw.text;
StopCoroutine("doLogin");
}else{
logmessage +="Error : " + logw.error + "\n";
}
if(logmessage == "Login success! Please wait while the game loads..."){
Network.Connect("avct.sytes.net", 8632);
}
}
private IEnumerator ServerRefresh()
{
yield return new WaitForSeconds(3);
MasterServer.RequestHostList("MMOFPS");
}
}
using UnityEngine;
using System.Collections;
public class CharacterManager : MonoBehaviour
{
private string message = "";
private string userName = "";
private string mainDomainName= "http://avct.sytes.net/mmorpg";
//Chequea si tiene personaje
private int existCharacter;
public int UserID;
public GameObject characterInfo;
public GameObject createCharacter;
void Start()
{
characterInfo.gameObject.SetActive(false); // Desactivo Script characterInfo
createCharacter.gameObject.SetActive(false); // Desactivo Script createCharacter
userName = PlayerPrefs.GetString ("Player Name");
StartCoroutine("CharacterCheck", 2.0F);
}
private IEnumerator CharacterCheck()
{
WWWForm userInfoForm = new WWWForm();
userInfoForm.AddField("Username" , userName);
var logw = new WWW(mainDomainName + "/CharacterCheck.php", userInfoForm);
yield return logw;
if(logw.error == null){
string[] data = logw.text.Split(new string[] {","}, System.StringSplitOptions.None);
UserID = System.Convert.ToInt32(data[0]);
existCharacter = System.Convert.ToInt32(data[1]);
yield return new WaitForSeconds(4);
if(existCharacter == 1)//Si Tiene Personaje
{
createCharacter.gameObject.SetActive(false); // Desactivo Script createCharacter
characterInfo.gameObject.SetActive(true); // Desactivo Script characterInfo
}
if(existCharacter == 0)//No Tiene Personaje
{
characterInfo.gameObject.SetActive(false); // Desactivo Script characterInfo
createCharacter.gameObject.SetActive(true); // Desactivo Script createCharacter
}
}else{
message +="Error : " + logw.error + "\n";
}
}
}