- Home /
Unity crashes when doing an RPC.
Hey all, I'm trying to do a RPC but both the Unity Designer and Unity WebPlayer crash whenever it tries to do it. I therefore have no way of knowing what im doing wrong.
The GameObject
that is running the below code is created by`Network.Instantiate` from a prefab with a NetworkView
already attached.
Here is my update code:
void FixedUpdate()
//Unrelated code generating vehicles wheel movement.
if(networkView.isMine)
{
SetInputHorizontal(Input.GetAxisRaw("Horizontal"));
SetInputHorizontal(Input.GetAxisRaw("Vertical"));
}
//Unrelated code setting wheel movement.
}
Here are the RPC functions:
[RPC]
public void SetInputVertical(float InputVal)
{
if(networkView.isMine && vertInput != InputVal)
{
networkView.RPC("SetInputVertical", RPCMode.All, InputVal);
}
vertInput = InputVal;
}
[RPC]
public void SetInputHorizontal(float InputVal)
{
if(networkView.isMine && horizInput != InputVal)
{
networkView.RPC("SetInputHorizontal", RPCMode.All, InputVal);
}
horizInput = InputVal;
}
The game will run, but whenever I press an input key triggering the change in input, thus triggering the RPC, Unity totally crashes.
Anyone got an idea as to what I'm doing wrong?
Answer by syclamoth · Jan 11, 2012 at 03:29 AM
Take a look at this function-
[RPC]
public void SetInputVertical(float InputVal)
{
if(networkView.isMine && vertInput != InputVal)
{
networkView.RPC("SetInputVertical", RPCMode.All, InputVal);
}
vertInput = InputVal;
}
What does it do?
First, it checks to see if the networkView is local. If it is, it sends an RPC to everyone, and finishes.
However, 'everyone' includes the local machine! So every time this RPC gets called, it recursively calls itself again! So, every time you call this function (RPC or not), it will create an infinite loop.
You should use RPCMode.Others, instead.
(I once had a similar problem, except that it only manifested if there were at least three clients connected! That one took a while to fix, let me tell you.)
Oh ok, I was told that RPC$$anonymous$$ode.All doesn't call itself. I guess I'll change it to RPC$$anonymous$$ode.Others
then and see if it fixes it.
PERFECT! It works great. $$anonymous$$y character drives around just like he were a local player.