Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FunIsDangerous · Jun 12, 2017 at 08:59 AM · errorgameobjectnetworkingprefabphoton

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ChristianSimon · Jun 12, 2017 at 09:29 AM 0
Share

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.

avatar image FunIsDangerous ChristianSimon · Jun 12, 2017 at 09:47 AM 0
Share

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

avatar image ChristianSimon FunIsDangerous · Jun 12, 2017 at 10:16 AM 0
Share

Pretty sure this wouldn't have compiled at all if it was the problem

Obviously yes, you are right.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FunIsDangerous · Jun 12, 2017 at 03:59 PM 0
Share

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 :)

avatar image ChristianSimon FunIsDangerous · Jun 15, 2017 at 08:51 AM 0
Share

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.

avatar image FunIsDangerous ChristianSimon · Jun 15, 2017 at 10:15 AM 0
Share

Oh well, that, for some reason, was not in any tutorials I read. Thanks again!

avatar image M4R5 · Nov 21, 2019 at 10:39 PM 0
Share

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)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FunIsDangerous · Jun 24, 2017 at 03:54 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges