- Home /
Issues with PhotonView
I've been searching all day so far, and I could find a solution. Here's some code that connects to the Photon servers, joins a room and spawns a player. On the player, there is a RigidbodyFirstPersonController script that I want to disable for remote players so a user can only control their own player. Here's the code so far: using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson;
public class WorldNetworking : MonoBehaviour {
public string playerPrefab;
// Use this for initialization
void Start()
{
PhotonNetwork.ConnectUsingSettings("0.1");
}
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
void OnJoinedLobby ()
{
RoomOptions roomOptions = new RoomOptions() { isVisible = true, maxPlayers = 500 };
PhotonNetwork.JoinOrCreateRoom("Default", roomOptions, TypedLobby.Default);
}
void OnPhotonJoinRoomFailed (object[] reasonForFailure)
{
//Example: void OnPhotonJoinRoomFailed(object[] codeAndMsg) { // codeAndMsg[0] is int ErrorCode. codeAndMsg[1] is string debug msg. }
Debug.Log ("Failed to join room with code " + reasonForFailure[0] + " message " + reasonForFailure[1] + ". Retrying.");
OnJoinedLobby ();//Try again.
}
void OnJoinedRoom()
{
SpawnPlayer ();
}
private void SpawnPlayer()
{
Debug.Log ("SpawnPlayer");
GameObject MyPlayer = PhotonNetwork.Instantiate(playerPrefab, new Vector3(2530f, 8f, 2510f), Quaternion.identity, 0);
PhotonView myPhotonView;
myPhotonView = MyPlayer.GetComponent(PhotonView);
if (myPhotonView.isMine) {
RigidbodyFirstPersonController playerControl = MyPlayer.GetComponent<RigidbodyFirstPersonController> ();
playerControl.enabled = true;
}
}
// Update is called once per frame
void Update () {
}
}
However, this throws the errors
Assets/WorldNetworking.cs(44,54): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
Assets/WorldNetworking.cs(44,41): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments.
Assets/WorldNetworking.cs(44,41): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'
I then changed PhotonView myPhotonView; myPhotonView = MyPlayer.GetComponent(PhotonView); to
new PhotonView myPhotonView = MyPlayer.GetComponent(PhotonView);
which resulted in
Assets/WorldNetworking.cs(43,43): error CS1525: Unexpected symbol `myPhotonView', expecting `(', `)', `,', `;', `[', `{', or `<operator>'
I am left clueless, and hoping someone on this community could help.
I might be able to fix this by changing the code to
myPhotonView = new PhotonView;
myPhotonView = $$anonymous$$yPlayer.GetComponent(PhotonView);
But I haven't tested it as of yet.
EDIT: This didn't work.
Answer by Jackmawer · Apr 09, 2015 at 07:50 PM
I tried hbalint1's answer and found that it had another error:
The type arguments for method `UnityEngine.GameObject.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly
so, I changed the code to
PhotonView myPhotonView;
myPhotonView = MyPlayer.GetComponent<PhotonView>();
and it worked without errors. Thanks for your help, hbalint1!
Answer by hbalint1 · Apr 09, 2015 at 03:23 PM
change
myPhotonView = MyPlayer.GetComponent(PhotonView);
to
myPhotonView = MyPlayer.GetComponent<PhotonView>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613192534im_/https://answers.unity.com/themes/thub/images/avi.jpg)