- Home /
How do I GetComponent from a NetworkView?
The problem I am having is trying to get a component on a prefab that is connected through the NetworkView. Is this possible? What do I need to do exactly?
I have a Player Prefab that has a PlayerLabel on it which controls the displaying of the PlayerName. It also has a NetworkView on it.
I have a SpawnManager that sends each player's (GameObject)Network.Instantiate object to the NetworkManager.
The NetworkManager Is attached to the "GameGo" prefab which also has a NetworkView component.
I am trying to send an RPC in the NetworkManager class that will take the Network.Instantiated object's ViewID, get the GameObject associated with this ViewID, find the "PlayerLabel" component on it, find the "NetworkManager" component on it (which is in gamego), and set the PlayerLabel PlayerName to the one on the NetworkManager.
This is what I have so far. This is my function that will get the spawned player Object with Network.Instantiate.
void OnServerInitialized ()
{
Debug.Log("Server is initialzed.");
var obj = GameObject.Find("SpawnManagerGO");
var script = obj.GetComponent<SpawnScript>();
GameObject player = script.spawnPlayer();
networkView.RPC("Rename", RPCMode.All, player.networkView.viewID, player.name);
}
This is the RPC function that I try to get the PlayerName with the NetowrkManager on GameGo.
[RPC]
void Rename(NetworkViewID ID, string nm)
{
//get the network view with the nv id
NetworkView nv = NetworkView.Find(ID);
//get the gameobject with the network view
GameObject go = GameObject.Find(nm);
var plScript = go.GetComponent<PlayerLabel>();
var goScript = nv.GetComponent<NetworkManager>();
Debug.Log (goScript.PlayerName);
plScript.PlayerName = goScript.PlayerName;
go.name = goScript.PlayerName;
}
This is Unity answers for you. Two years and still not answered. Same problem, if I find a fix I'll give you an answer.