Search code examples
c#unity-game-enginemultiplayerphotonphoton-pun

Photon Networking Instantiate Error (Unity3d)


Ok, so I'm making an online FPS in Unity and I was scripting that Photon Networking Script to connect and spawn the player and I keep getting these two errors:

Assets/Resources/GameManager.cs(64,23): error CS1502: The best overloaded method match for `PhotonNetwork.Instantiate(string, UnityEngine.Vector3, UnityEngine.Quaternion, int)' has some invalid arguments
Assets/Resources/GameManager.cs(64,23): error CS1503: Argument `#1' cannot convert `UnityEngine.Transform' expression to type `string'

Here is where the error is in my code:

// When Connected [Photon Callback]
void OnJoinedRoom()
{
    PhotonNetwork.Instantiate(playerPrefab, transform.position, Quaternion.identity, 0);
}

//In Game: Disconnect from room.
void InGameGUI()
{
    if (GUILayout.Button("Leave Game"))
        PhotonNetwork.LeaveRoom();
}

And I did reference the Transform at the top:

public Transform playerPrefab;

Any ideas on what I did wrong and how I could fix it. Please help!


Solution

  • PhotonNetwork.Instantiate requires a string, not a Transform object as it's first parameter. (I do believe this was changed from a Transform object a while ago). Simply name the prefab that you want to instantiate (which must be in the Resources folder).