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");
			}
	}

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!

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.