# InvalidCastException when instantiating a prefab on the network using Photon

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:

• Your image displays as thumbnail with ultra-low resolution, maybe something went wrong during upload of image? – wondra Jun 11 '17 at 9:34
• What type is gunToEquip? How do you assign it? – S. Tarık Çetin Jun 11 '17 at 10:20
• Not sure what went wrong with the picture, here is a link to it: imgur.com/a/GwGq3. – Fanis Deligiannis Jun 11 '17 at 11:11
• 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.) – doppelgreener Jun 11 '17 at 11:53
• 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. – Fanis Deligiannis Jun 11 '17 at 12:46