I’ve since figured out the issue, still I’m awful at Unity Networking and Networking in general so feel free to tell me I’m doing something wrong.
What it is I’m making (not really important): I’m making a chat system. The system is designed to only show one message at a time for a short duration. Kind of a throwback to singleplayer games where you press a button for the next line of dialog, only it’s running on a timer. Pretty impractical for lots of people talking at once but I’ll make it a local radius thing sometime later.
What I need help with: Basically when a player presses return in an input field it changes the message shown by the chat log. I also want it to prompt the message window by changing the RectTransform
for all clients chat log, so I felt using a SyncVar Hook
, unless it only does it on client-side? (I am quite awful at understanding how networking works). Anyway, when I declare [SyncVar(Hook="OnChangeMessage")]
Unity throws a few errors, most notably “SyncVar Hook function OnChangeMessage must have one argument ServerChat”.
Declaring
public class ServerChat : NetworkBehaviour
{
[SyncVar]
public string m_sender = "Sender";
[SyncVar(hook = "OnChangeMessage")]
public string m_message = "Message";
....
}
Using
public void ChangeMessage()
{
if (!isServer)
return;
if(m_inputField.text.Contains("\n"))
{
m_message = m_inputField.text;
m_inputField.ActivateInputField();
m_inputField.text = string.Empty;
}
}
Hook Method
private void OnChangeMessage()
{
rTransform.position = startPosition;
timer = interval; //reset the timer so the message window can scroll again
m_message_message.text = m_message;
}