- Home /
InvalidCastException when Instantiating with photon (PUN)
As stated in the title, I get this error when I try to instantiate a gameobject from a prefab, using PUN:
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;
Where:
equippedGun is by default an empty variable of type Gun
gunToEquip is a GameObject variable (with the component 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 do GetComponent instead of "as Gun", as normal instantiate returns Object and PhotonNetwork.Instantiate returns GameObject)
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. Any solutions?
EDIT: not sure if it is the problem, but here are the prefab settings: http://imgur.com/a/GwGq3
Hi,
as far as I can see there is the problem, that equippedGun is a GameObject and you try to apply a type of a Gun object to it. So you either have to remove the GetComponent() call after the PhotonNetwork.Instantiate(...) call or you have to change the type of equippedGun to a type of Gun object.
Pretty sure this wouldn't have compiled at all if it was the problem, but it's not, equippedGun is type Gun. It is my fault for not providing enough information,, but even doing it without saving it to a variable will throw this error
Pretty sure this wouldn't have compiled at all if it was the problem
Obviously yes, you are right.
Answer by ChristianSimon · Jun 12, 2017 at 11:54 AM
Hi,
me again :D
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.
Thanks! It's weird how I didn't find anything googling myself :P It still does not work for some group values(such as 1), but it does not throw an error. This is good enough for now at least, waiting for the update :)
Do you subscribe to the used Interest Group? You have to call PhotonNetwork.SetInterestGroups(byte group, bool enabled);
or PhotonNetwork.SetInterestGroups(byte[] disableGroups, byte[] enableGroups);
to subscribe and unsubscribe from groups you want to use or don't want to use any longer. You have to do this before calling PhotonNetwork.Instantiate(...);
with a group that is not 0, which is the default group.
Oh well, that, for some reason, was not in any tutorials I read. Thanks again!
Getting this issue in a project that's currently in 2018.3
InvalidCastException: Specified cast is not valid. Photon.Pun.PhotonTransformViewPositionControl.DeserializeData (Photon.Pun.PhotonStream stream, Photon.Pun.Photon$$anonymous$$essageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformViewClassic.cs:391) Photon.Pun.PhotonTransformViewPositionControl.OnPhotonSerializeView (UnityEngine.Vector3 currentPosition, Photon.Pun.PhotonStream stream, Photon.Pun.Photon$$anonymous$$essageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformViewClassic.cs:369) Photon.Pun.PhotonTransformViewClassic.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.Photon$$anonymous$$essageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformViewClassic.cs:129) Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.Photon$$anonymous$$essageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:351) Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, Photon.Pun.Photon$$anonymous$$essageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:341) Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, System.Int32 networkTime, System.Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1670) Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2040) Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2785) ExitGames.Client.Photon.PeerBase.Deserialize$$anonymous$$essageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at C:/Dev/photon-sdk-dotnet/PhotonDotnet/PeerBase.cs:640) ExitGames.Client.Photon.EnetPeer.DispatchInco$$anonymous$$gCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotnet/EnetPeer.cs:552) ExitGames.Client.Photon.PhotonPeer.DispatchInco$$anonymous$$gCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotnet/PhotonPeer.cs:1548) Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:205) Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:139)
Answer by TheStarboxTR · Jun 23, 2017 at 10:55 PM
hi guys :)
ı tryed to change type of group parameter int to byte but it didnt change anything at all? when ı try to instantiate an object,nothing happens now. no error message or no instantiated object.
ı gave up to using photon's instantiate function and ı wrote myself.it is working but ı want to use photon's function.
As said above, you need to be using be default group 0, if you want any other group, each client has to subscribe to that using:
PhotonNetwork.SetInterestGroups(byte group, bool enabled);
Your answer
Follow this Question
Related Questions
What does "Couldn't find matching instance in prefab" mean? 2 Answers
Photon error nentworking 0 Answers
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
InvalidCastException: Specified cast is not valid. (NETWORKING) 1 Answer
Networking troubles with prefab 0 Answers