- Home /
How do i access a network instantiated object?
Hi i am making a network game and for now i want to identify the players by colour/texture. I don't want to change the prefab every time that someone spawns in the game so the only way i can see to do it is change it after i have ran Network.Instantiate().
Is it possible to get an object after this and then change the texture/material used or even just the colour?
If that is not possible is there anyway to instantiate to object to change it and put it on the "network" in another way?
Answer by syclamoth · Nov 18, 2011 at 02:31 PM
Network.Instantiate automatically sets up network views on all clients. This means that you can set up a script on any individual object, that goes something like this-
public Color myColour = set it however you need to on the client;
if(networkView.isMine)
{
networkView.RPC("SetColour", RPCMode.All, myColour.r, myColour.g, myColour.b);
}
// elsewhere
[RPC]
void SetColour(float r, float g, float b)
{
myColour = new Color(r, g, b);
}
Thanks for the answer i knew there had to be something. But this isn't working for me. Would this be because it is a prefab, basically the same as the Character controller in Standard Assets, and the $$anonymous$$aterial/colour is controlled by the the Controller->Graphics->$$anonymous$$esh Renderer->$$anonymous$$aterials?
Yes, that would be it. Give your script a reference to the mesh renderer (since it's not on the same object), and use that ins$$anonymous$$d. $$anonymous$$y code doesn't specify how you use the Color object once it's been passed around- that's up to you.
mmm now I'm even more confused and still can't get it working, i'm not sure how i would reference the mesh renderer from the code, i have tried
character.GetComponentInChildren<$$anonymous$$eshRenderer>()
but it doesn't work, how do i reference it and then apply the new colour?