- Home /
Unet - adding [Command] function to Action C#
I'm working in Unity with Unet and I have one question:
-Can I add command functions directly to an Action?
Aparently I can't, so my code is throwing the error "RPC Function RpcNextMaterial called on client".
I fixed my code to works, but doing something that seems "ugly": Instead of adding to my Action.OnInteraction the Cmd function, I have built a function that calls the CMD function.
So the line of code fixed is this:
this.OnInteraction += NmCmdNextMaterial;
But should be just:
this.OnInteraction += CmdNextMaterial;
This is the code that is currently working, just fires the "nextMaterial" function when the user (server or client) clicks on it.
public class InteractiveObject : NetworkBehaviour, IPointerClickHandler {
private Action OnInteraction;
//when the user clicks on GameObject collider
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
nextMaterial();
}
}
//callback fired on the client when the network authority changes
public override void OnStartAuthority()
{
if (!isServer)
{
if(OnInteraction != null)
{
OnInteraction();
OnInteraction = null;
}
}
}
//Set Next Material
#region nextMaterial
private void nextMaterial()
{
if (isServer)
{
RpcNextMaterial();
}
else
{
if (!hasAuthority)
{
//this.OnInteraction += CmdNextMaterial; // THIS IS WHAT I WANT
this.OnInteraction += NmCmdNextMaterial;
}
else
{
CmdNextMaterial();
}
}
}
public void NmCmdNextMaterial()
{
CmdNextMaterial();
}
[Command]
public void CmdNextMaterial()
{
RpcNextMaterial();
}
[ClientRpc]
private void RpcNextMaterial()
{
//change Material code
}
#endregion
}
Your answer
Follow this Question
Related Questions
Do i need to have 2 seperate apps communicating for server/client relationship? 2 Answers
UNet changing player prefabs int the network lobby sample. 0 Answers
Different movement speeds over network 1 Answer
Network command is running on client? 1 Answer
Trying to send command for object without authority. - change color of object from client 0 Answers