As stated in the title, I get this error when I try to instantiate a game object using Photon:

InvalidCastException: Cannot cast from source type to destination type.

NetworkingPeer.DoInstantiate (ExitGames.Client.Photon.Hashtable evData, .PhotonPlayer photonPlayer, UnityEngine.GameObject resourceGameObject) (at Assets/Photon/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3069)

PhotonNetwork.Instantiate (System.String prefabName, Vector3 position, Quaternion rotation, Int32 group, System.Object[] data) (at Assets/Photon/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2493)

PhotonNetwork.Instantiate (System.String prefabName, Vector3 position, Quaternion rotation, Int32 group) (at Assets/Photon/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2437)

GunController.EquipGun (.Gun gunToEquip) (at Assets/Scripts/Gun/GunController.cs:38)

GunController.init () (at Assets/Scripts/Gun/GunController.cs:19)

NetworkPlayer.Start () (at Assets/Scripts/Network/NetworkPlayer.cs:20)

Old, working code:

equippedGun = Instantiate(gunToEquip, weaponHold.position, weaponHold.rotation) as Gun;
equippedGun.transform.parent = weaponHold;

New code, using Photon:

equippedGun = PhotonNetwork.Instantiate(gunToEquip.name, weaponHold.position, 
                                        weaponHold.rotation, 1).GetComponent<Gun>();
equippedGun.transform.parent = weaponHold;

(I do GetComponent, as normal instantiate returns Object and PhotonNetwork.Instantiate returns GameObject)

  • equippedGun is by default an empty variable of type Gun

  • gunToEquip is again a Prefab variable of type Gun that gets passed into the function. (PhotonNetwork.Instantiate takes in the name of the prefab, not the actual prefab, thus gunToEquip.name)

    In this particular case, gunToEquip is the default gun that fails to instantiate, which is set by the unity inspector

  • weaponHold is just a transform variable, again set by the inspector.

I somehow managed to fix it for my player prefab, I have no idea how. I was just changing and reverting stuff until it magically worked. How can I resolve this error?

Not sure if it is the problem, but I've attached the prefab settings:

enter image description here

  • 1
    \$\begingroup\$ Your image displays as thumbnail with ultra-low resolution, maybe something went wrong during upload of image? \$\endgroup\$ – wondra Jun 11 '17 at 9:34
  • \$\begingroup\$ What type is gunToEquip? How do you assign it? \$\endgroup\$ – S. Tarık Çetin Jun 11 '17 at 10:20
  • \$\begingroup\$ Not sure what went wrong with the picture, here is a link to it: imgur.com/a/GwGq3. \$\endgroup\$ – Fanis Deligiannis Jun 11 '17 at 11:11
  • \$\begingroup\$ This is a type casting exception, but we don't know the types of those variables you're passing. Could you convey to use their types, how they're declared, and how the classes you're using are defined? (I'm voting to close this as not containing complete enough information to resolve the issue until then.) \$\endgroup\$ – doppelgreener Jun 11 '17 at 11:53
  • \$\begingroup\$ Added a little more info about each variable used. I don't think it is the problem though, since the same happened at a completely different piece of code, on my player Prefab. \$\endgroup\$ – Fanis Deligiannis Jun 11 '17 at 12:46

As per an answer I got on Unity answers:

There is a little bug in the current PUN version which will be fixed with the next update. For now you can apply this fix yourself.

Open PhotonNetwork class and look for all variants of the Instantiation function. These functions can be found (PUN version 1.84) in line 2435, line 2450 and line 2510. For each of this three functions look for the group parameter and change its type from int to byte.

Now open the NetworkingPeer class and look for SendInstantiate function. In PUN 1.84 this function can be found in line 3001. Do the same like before and change the type of the group parameter from int to byte.

It worked like a charm, I'm leaving it here so other people with this problem can find it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.