- Home /
PhotonView over GameObject
Hey guys, I am trying to get a peace of code to work. The problem with converting from Unity Network to Photon is that for some reason I get an error when trying to call the photonView from the last player variable because its a GameObject (which works over networkView, the normal unity networking). I'll list both scripts, one from the Photon and one from the Unity MasterServer, because I think that might help. Any help is appreciated, thank you in advance!
Unity MasterServer (works):
[RPC]
public void Client_PlayerJoined(string Username, NetworkPlayer id)
{
Player temp = new Player();
temp.PlayerName = Username;
temp.OnlinePlayer = id;
PlayerList.Add(temp);
if(Network.player == id)
{
MyPlayer = temp;
GameObject LastPlayer = Network.Instantiate(SpawnPlayer, Vector3.zero, Quaternion.identity, 0) as GameObject;
LastPlayer.networkView.RPC("RequestPlayer", RPCMode.AllBuffered, Username);
temp.Manager = LastPlayer.GetComponent<UserPlayer>();
Debug.Log("Spawned Player");
}
}
PhotonCloud Version (Error: Type UnityEngine.GameObject' does not contain a definition for
photonView' and no extension method photonView' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?))
[RPC]
public void Client_PlayerJoined(string Username, PhotonPlayer id)
{
Player temp = new Player();
temp.PlayerName = Username;
temp.OnlinePlayer = id;
PlayerList.Add(temp);
if(PhotonNetwork.player == id)
{
MyPlayer = temp;
GameObject LastPlayer = PhotonNetwork.Instantiate("RigidBodyPlayer", Vector3.zero, Quaternion.identity, 0) as GameObject;
LastPlayer.photonView.RPC("RequestPlayer", PhotonTargets.AllBuffered, Username);
temp.Manager = LastPlayer.GetComponent<UserPlayer>();
MyPlayer.PlayerName.photonView.RPC("RequestPlayer", PhotonTargets.AllBuffered, Username);
Debug.Log("Spawned Player");
}
}
Answer by OP_toss · Dec 15, 2013 at 08:15 PM
The error seems to tell you exactly what's wrong. There is no definition for "photonView" on a GameObject. So instead, get the photon view component using gameObject.GetComponent(), assuming that's the correct type name. Then call your RPC on that.
Also I assume you know that the prefab "RigidBodyPlayer" needs to have a PhotonView attached for you to be able to Network.Instantiate it.
Hope this helps!
Thanks for your reply! That solves half of the question because it worked :) The second is how to get the networkView via string ($$anonymous$$yPlayer.PlayerName). Do you have any idea of how to do that?
I'm not sure you need to, but assu$$anonymous$$g you do, use GameObject.Find( "GameObjectName" ).GetComponent().
I don't suggest using GameObject.Find. I can almost gaurantee with a little rework, you can avoid it entirely. You can send networkViewIDs via RPCs in uLink. Don't know about Photon. You can keep a dictionary of players, then lookup the player gameobject by name. etc. etc.
Also please mark correct if this solved your problem. Thanks!
Yeah it works thanks, I recon there is an error with my code but thanks anyway! What I ended up doing was making a transform part of "$$anonymous$$yPlayer" and using that to photonView.GetComponent().
Answer by Meatgrind · Sep 29, 2014 at 08:15 AM
Photon Unity Networking (PUN) expects you to inherit your classes from their extended Photon.MonoBehaviour like this:
public class MyClass : Photon.MonoBehaviour //...
Their MonoBehaviour is extended to cache this.photonView so you don't need to do the GetComponent setup for every script that uses PhotonView. Of course, as previous answer mentioned, the PhotonView component has to be attached to the prefab you are trying to PhotonNetwork.Instantiate.