I am making a chat app with Unity and I have created two scripts one that connects with the server and the other that provides gives the chat funcionlaity. I have attached both of the scripts to my Main Camera but the problem is that only one scripts works but the other doesn’t. Please help me with a fix.
There are no errors.
Working Script :
using System.Collections.Generic;
using UnityEngine;
public class NetworkMenu : MonoBehaviour {
public string connectionIP = "127.0.0.1";
public int portNumber = 9192;
public static bool connected { get; private set; }
private void OnConnectedToServer()
{
connected = true;
}
private void OnServerInitialized()
{
connected = true;
}
private void OnDisconnectedFromServer()
{
connected = false;
}
public void OnGUI()
{
if (!connected)
{
connectionIP = GUILayout.TextField(connectionIP);
int.TryParse(GUILayout.TextField(portNumber.ToString()), out portNumber);
if (GUILayout.Button("Connect"))
{
Network.Connect(connectionIP, portNumber);
}
if (GUILayout.Button("Host"))
{
Network.InitializeServer(4, portNumber, true);
}
}
else
{
GUILayout.Label("Connections: " + Network.connections.Length.ToString());
}
}
}
Script Not working at all and not even giving a message in Logs:
using System.Collections;
using System.Collections.Generic;
public class Chat : MonoBehaviour {
public List<string> chatHistory = new List<string>();
private string currentMessage = string.Empty;
private void onGUI(){
if (!NetworkMenu.connected) {
return;
}
GUILayout.BeginHorizontal (GUILayout.Width (250));
currentMessage = GUILayout.TextField (currentMessage);
if (GUILayout.Button("Send")){
if (!string.IsNullOrEmpty(currentMessage.Trim())){
GetComponent<NetworkView>().RPC("ChatMessage", RPCMode.AllBuffered, new object[] { currentMessage });
currentMessage = string.Empty;
}
}
GUILayout.EndHorizontal();
foreach (string x in chatHistory){
GUILayout.Label(x);
}
}
[RPC]
public void ChatMessage(string Message){
chatHistory.Add (Message);
}
}
Screenshot: