Hi!
Ive got a game with chat and this is my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Chat : NetworkBehaviour {
string MessageHistory;
private string currentMessage = string.Empty;
void Update(){
GameObject.Find ("ChatText").GetComponent<Text>().text = MessageHistory;
}
public void SEND(){
currentMessage = GameObject.Find ("SENDMSG").GetComponent<InputField>().text;
Cmdcallchat (PlayerPrefs.GetString("GamerTag")+" Says: "+ currentMessage);
currentMessage = string.Empty;
GameObject.Find ("SENDMSG").GetComponent<InputField> ().text = "";
}
[Command]
private void Cmdcallchat(string msg){
RpcChatMessage (msg);
}
[ClientRpc]
public void RpcChatMessage(string message){
print (message);
MessageHistory = MessageHistory + "\n" + message;
}
// Use this for initialization
void Start () {
}
}
but only the server can send messages and the clients can only see its messages but not send their own (as in when they press enter on the text box it doesn’t show anything text box calls SEND() function) why is this?
Where is SEND() called?
If it’s not being called in the right place, that would explain the odd behaviour.
I otherwise don’t see any obvious flaws.
send is now getting called when I press enter this is a new script I’m just using GUIlayout now:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Chat : NetworkBehaviour {
string MessageHistory;
public string currentMessage = string.Empty;
private string LastTroll = "internet explorer";
void Update(){
if(isLocalPlayer){
if (Input.GetKeyDown(KeyCode.Return)){
SEND ();
}
}
GameObject.Find ("ChatText").GetComponent<Text> ().text = MessageHistory;
}
void OnGUI() {
if (isLocalPlayer){
currentMessage = GUI.TextField(new Rect(10, 10, 200, 20), currentMessage, 25);
}
}
public void SEND(){
print ("SEND FUNCTION");
//set pass varible to true
var Pass = true;
/*foreach(string Word in BadWords){
if (currentMessage.Contains (Word) == true) {
Pass = false;
}
}*/
//Detect for any "bad" words if there is set pass to false
if (currentMessage.Contains ("fuck") == true) {
Pass = false;
} else {
if (currentMessage.Contains ("shit") == true) {
Pass = false;
} else {
if (currentMessage.Contains ("bitch")) {
Pass = false;
}
}
}
//detect if pass = true and send message else ... i dont need to explain
if (Pass == true) {
Cmdcallchat (PlayerPrefs.GetString ("GamerTag") + " Says: " + currentMessage);
} else {
//set troll word
var TrollWord = "";
var TrollPass = false;
//set random int
var Randomselect = Random.Range (0,3);
//decide troll word upon the random int
while (TrollPass == false){
if (Randomselect == 1){
TrollWord = "tiny spud";
}else{
if (Randomselect == 2) {
TrollWord = "internet explorer";
} else {
TrollWord = "silly sausage";
}
}
if (TrollWord != LastTroll){
TrollPass = true;
}
LastTroll = TrollWord;
}
//troll for troll
Cmdcallchat ("Noob "+PlayerPrefs.GetString ("GamerTag")+" is being "+TrollWord);
}
//Clear up text fields n stuff
currentMessage = string.Empty;
}
[Command]
private void Cmdcallchat(string msg){
RpcChatMessage (msg);
print ("CMD CALLED");
}
[ClientRPC]
public void RpcChatMessage(string message){
print (message);
MessageHistory = MessageHistory + "\n" + message;
}
// Use this for initialization
void Start () {
}
}
as you can see when I press enter it calls SEND() but now the clients can send and see messages but only the most recent client can send and the server cant send only see
@FuzzyQuills
I seriously can’t see where it’s stuffing up when it comes to “only most recent client can send”
As for server messages however… I think the game server needs it’s own call, but being that I haven’t used UNET that much… I’ll look into it.
To be honest, this is why I still use the old system, let’s hope they’re not getting rid of that anytime soon… 
EDIT: This might help, since it’s designed to be called on the server: http://docs.unity3d.com/ScriptReference/Networking.ServerCallbackAttribute.html
Then again however, this is more for code that’s already running…
Ok thank you I will se what I can do but on the of chance you know this would the reason I cant do it be because its not on the player gameobject @FuzzyQuills
I wouldn’t think a game chat thing would be on a player gameobject…
That’s clearly some backwards UNET logic…