So I have an object that I am trying to manipulate through UNET. What I am doing is basically spawning an object onto the server and then I perform a ray cast, if the ray cast comes back and I hit a child of that spawned object then I want to change the material of it. Sort of like a highlighting effect. I can get the child object back fine and it is working all the way up until I call it using the Rpc. I am getting a warning that says something like:
Network Writer myChildObject has no Network Identity
and then it goes on to give me a Null reference error right after. After thinking about it, I believe it is giving me this error because that object can't be sent to other clients without a network Id but, its parent already has one and I can't give it one if its parent has one... Is there a workaround for this madness or am I going about this all wrong?
My code block on my PlayerController
is as follows:
[Command]
void CmdHighlight()
{
//get what the user hit and then if we hit something send it over the server to be highlighted
GameObject hitObject = ControllerUNET.CheckForHitObject();
if(hitObject != null)
RpcHighlight(hitObject);
}
[ClientRpc]
void RpcHighlight(GameObject hitObject)
{
//highlight the object over the server
//Throws null reference on game object on line below
ControllerUNET.HighlightHitObject(hitObject);
}
Well, after doing more research and testing I found that my initial conclusions were correct. You cannot send gameObjects over the server and Unity also does not support network ids on child objects of one that already has one. Instead I created a list of the children on the parent and then when I get the hit object I find its index in the list and send that over the server instead and the clients use that to get a reference to the object that was hit.