Multiplayer Chat Script (Problem : mixed names)

Hello :slight_smile:

I have problem with chat script. I watched tutorial and I have same script as on the tutorial. But I have problem.

The problem is it won’t show the right name.
Exaple.
I start the game. Enter name and when I start game the name is not showed. Next time I start the game and ENTER DIFFERENT name and when I run the game it showes me the name I typed in from last time I run the game. I hope you get it :confused:

Here is the full script :confused: (the guy from tutorial doesn’t respon so I ask you for help ok ! )

using UnityEngine;
using System.Collections;

public class Chat2 : MonoBehaviour
{
bool usingChat = false;
bool showChat = false;

string inputField = "";

Vector2 scrollposition;
int width = 500;
int height = 100;
string playerName;
float lastUnfocusTime = 0;
Rect window;

ArrayList playerList = new ArrayList();
class PlayerNode
{
    
    public string playerName;
  	public NetworkPlayer player;
}

ArrayList chatEntries = new ArrayList();
class ChatEntry
{
    public string name = "";
    public string text = "";
}

// Use this for initialization
void Start ()
{
    window = new Rect(Screen.width / 2 - width / 2, Screen.height - height + 5, width, height);
    playerName = PlayerPrefs.GetString("playerName", "");

    if (playerName == "") playerName = "RandomName" + Random.Range(1, 999);
	if (playerName == "playerName") playerName = "playerName";
}

void OnConnectedToServer()
{
    ShowChatWindow();
    networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
    addGameChatMessage(playerName + "has just joined the chat!");
}

void OnServerInitialized()
{
    ShowChatWindow();
    PlayerNode newEntry = new PlayerNode();
    newEntry.playerName = playerName;
    newEntry.player = Network.player;
    playerList.Add(newEntry);
    addGameChatMessage(playerName + "has just joined the chat!");
}
PlayerNode GetPlayerNode(NetworkPlayer netPlay)
{
    foreach(PlayerNode entry in playerList)
    {
        if (entry.player == netPlay)
            return entry;
    }
    Debug.LogError("GetPlayerNode: Requested a playernode of non-existing player!");
    return null;
}
void OnPlayerDisconnected(NetworkPlayer netPlayer)
{
    addGameChatMessage("A Player has disconnected");
    playerList.Remove(GetPlayerNode(netPlayer));

}
void OnDisconnectedFromServer()
{
    CloseChatWindow();
}

[RPC]
void TellServerOurName(string name, NetworkMessageInfo info)
{
PlayerNode newEntry = new PlayerNode();
newEntry.playerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + " has just joined the chat!");

}
void CloseChatWindow()
{
    showChat = false;
    inputField = "";
    chatEntries = new ArrayList();

}
void ShowChatWindow()
{
    showChat = true;
    inputField = "";
    chatEntries = new ArrayList();
}

void OnGUI()
{
    if(!showChat) return;

    if(Event.current.type == EventType.keyDown && Event.current.character == '

’ & inputField.Length <= 0)
{
if(lastUnfocusTime + .25f < Time.time)
{
usingChat = true;
GUI.FocusWindow(5);
GUI.FocusControl(“Chat input field”);
}
}

window = GUI.Window(5, window, GlobalChatWindow, "");
}

void GlobalChatWindow(int id)
{
    GUILayout.BeginVertical();
    GUILayout.Space(10);
    GUILayout.EndVertical();

    scrollposition = GUILayout.BeginScrollView(scrollposition);

    foreach (ChatEntry entry in chatEntries)
    {
        GUILayout.BeginHorizontal();
        if (entry.name == " - ")
            GUILayout.Label(entry.name + entry.text);

        GUILayout.EndHorizontal();
        GUILayout.Space(2);
    }

    GUILayout.EndScrollView();

    if(Event.current.type == EventType.keyDown && Event.current.character == '

’ & inputField.Length > 0)
HitEnter(inputField);
GUI.SetNextControlName(“Chat input field”);
inputField = GUILayout.TextField(inputField);

    if (Input.GetKeyDown("mouse 0"))
    {
            if( usingChat)
            {
                    usingChat = false;
                GUI.UnfocusWindow();
                lastUnfocusTime = Time.time;
            }
    }
  }   
void HitEnter(string msg)
{
        msg = msg.Replace('

', ’ ');
networkView.RPC(“ApplayGlobalChatTexT”, RPCMode.All, playerName, msg);
}
[RPC]
void ApplayGlobalChatText(string name, string msg)
{
ChatEntry entry = new ChatEntry();
entry.name = name;
entry.text = msg;

    chatEntries.Add(entry);

    if(chatEntries.Count > 4)
        chatEntries.RemoveAt(0);

    scrollposition.y = 1000000;
    inputField = "";
}
void addGameChatMessage(string str)
{
    ApplayGlobalChatText(" - ", str);
    if(Network.connections.Length > 0)
        networkView.RPC("ApplayGlobalChatTexT", RPCMode.Others, " - ", str);
}

}

Also I figure it out that this line is responsible for displaying the name

void Start ()
{
window = new Rect(Screen.width / 2 - width / 2, Screen.height - height + 5, width, height);
playerName = PlayerPrefs.GetString(“playerName”, “”);

if (playerName == "") playerName = "RandomName" + Random.Range(1, 999);
if (playerName == "playerName") playerName = "playerName";

}

I hope you understand my problem and that you can help me. :smiley:

You should learn some more C# scripting before coming and asking people for help. I recommend you don’t Copy/Paste other peoples scripts from video becuase they will get you no where in developing games. To learn Javascript and or C# programming, I would go ann read the free tutorials on the Unity3D website, their very helpful.

In your case, this is just a simple error, I don’t know enough of C# scripting to go into detail, but I would change this:

void Start window = GUI.Label (new Rect(x,y,width,height), “PlayerName”);); playerName = PlayerPrefs.GetString(“playerName”, “”);