Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 baslq432 · Mar 03 at 08:58 PM · unity 5photononline game

InvalidCastException: Specified cast is not valid

I tride to be Allowed change player avatar when I press left arrow or right arrow but there is this error:

 InvalidCastException: Specified cast is not valid. PlayerItem.OnClickRightArrow () (at Assets/All/Script/PlayerItem.cs:59) UnityEngine.Events.InvokableCall.Invoke () (at :0) UnityEngine.Events.UnityEvent.Invoke () (at :0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385), I tride to be Allowed change player avatar when you prees  left arrow or right arrow but there is this erroR : InvalidCastException: Specified cast is not valid. PlayerItem.OnClickRightArrow () (at Assets/All/Script/PlayerItem.cs:59) UnityEngine.Events.InvokableCall.Invoke () (at :0) UnityEngine.Events.UnityEvent.Invoke () (at :0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)

PlayerItem.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using Photon.Pun;
 using Photon.Realtime;
 
 public class PlayerItem : MonoBehaviourPunCallbacks
 {
     public Text playerName;
 
     Image backgroundImage;
     public Color highlighColor;
     public GameObject leftArrowButton;
     public GameObject RightArrowButton;
 
     ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();
 
     public Image playerAvatar;
     public Sprite[] avatars;
 
     Player player;
     
     private void Awake()
     {
         backgroundImage = GetComponent<Image>();
     }
 
     public void SetPlayerInfo(Player _player)
     {
         playerName.text = _player.NickName;
         player = _player;
         UpdatePlayerItem(player);
     }
 
     public void ApplyLocalChanges()
     {
         backgroundImage.color = highlighColor;
         leftArrowButton.SetActive(true);
         RightArrowButton.SetActive(true);
     }
 
 
     public void OnClickLeftArrow()
     {
         if ((int)playerProperties["playerAvatar"] == 0)
         {
             playerProperties["playerAvatar"] = avatars.Length - 1;
         }
         else
         {
             playerProperties["playerAvatar"] = (int)playerProperties["playerAvatar"] - 1;
         }
         PhotonNetwork.SetPlayerCustomProperties(playerProperties);
     }
 
     public void OnClickRightArrow()
     {
         if ((int)playerProperties["playerAvatar"] == avatars.Length - 1)
         {
             playerProperties["playerAvatar"] = 0;
         }
         else
         {
             playerProperties["playerAvatar"] = (int)playerProperties["playerAvatar"] + 1;
         }
         PhotonNetwork.SetPlayerCustomProperties(playerProperties);
     }
 
     public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable CustomProperties)
     {
         if(player == targetPlayer)
         {
             UpdatePlayerItem(targetPlayer);
         }
     }
 
     void UpdatePlayerItem(Player player)
     {
         if(player.CustomProperties.ContainsKey("playerAvatar"))
         {
             playerAvatar.sprite = avatars[(int)player.CustomProperties["playerAvatar"]];
             playerProperties["playerAvatar"] = avatars[(int)player.CustomProperties["playerAvatar"]];
         }
         else
         {
             playerProperties["playerAvatar"] = 0;
         }
     }
 }


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Anox · Mar 03 at 09:08 AM

So at some point a cast goes wrong.

   public void OnClickRightArrow()
  {
      if ((int)playerProperties["playerAvatar"] == avatars.Length - 1)

In the above line you say that the result of

 playerProperties["playerAvatar"] 

must be convertible to type int. Hence the cast (int). Since the content of playerProperties["playerAvatar"] can not be converted to an int you get this exception.

Try this: right above your cast you can log the value an see if really can be converted to an int:

 Debug.Log("Is this a valid Int:"+playerProperties["playerAvatar"] );

Comment
Add comment · Show 8 · 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 baslq432 · Mar 03 at 12:24 PM 0
Share

@Anox its give me this error IndexOutOfRangeException: Index was outside the bounds of the array. PlayerItem.UpdatePlayerItem (Photon.Realtime.Player player) (at Assets/All/Script/PlayerItem.cs:87) PlayerItem.OnPlayerPropertiesUpdate (Photon.Realtime.Player targetPlayer, ExitGames.Client.Photon.Hashtable playerProperties) (at Assets/All/Script/PlayerItem.cs:78) Photon.Realtime.InRoomCallbacksContainer.OnPlayerPropertiesUpdate (Photon.Realtime.Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProp) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:4272) Photon.Realtime.LoadBalancingClient.ReadoutProperties (ExitGames.Client.Photon.Hashtable gameProperties, ExitGames.Client.Photon.Hashtable actorProperties, System.Int32 targetActorNr) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2236) Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3288) ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:899) ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:565) ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1771) Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:222) Rethrow as AggregateException: Caught 1 exception(s) in methods called by DispatchIncomingCommands(). Rethrowing first only (see above). Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:240) Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:145) when i but playerProperties["playerAvatar"] = avatars.Length;

avatar image Anox · Mar 03 at 12:39 PM 0
Share

I cannot tell what is standing on line: PlayerItem.cs:87

 playerProperties["playerAvatar"] = avatars.Length;

should not throw any IndexOutOfRange error because you are assigning a value and not fetching one.

 avatars[(int)player.CustomProperties["playerAvatar"]];

This code is also problematic and could very well throw the Error. You are accessing the array "avatars" with the key of HashTable player.CustomProperties["playerAvatar"]. That spells disaster :D

What value from "player.CustomProperties["playerAvatar"]." are you expecting and how big is "avatars"? I am guessing you are trying to access an index from "avatars" which is simply not there.

avatar image baslq432 Anox · Mar 03 at 01:21 PM 0
Share

@Anox I am do not understand what you meen but there is two sprite in avatars

avatar image Anox · Mar 03 at 03:12 PM 0
Share

So what is the value you recieve from: player.CustomProperties["playerAvatar"] ?

avatar image baslq432 Anox · Mar 03 at 04:54 PM 0
Share

@Anox it is on player code from photon

avatar image Anox baslq432 · Mar 03 at 09:29 PM 0
Share

wtf is that xD

Show more comments
avatar image
0

Answer by DaoGear · Mar 05 at 03:34 PM

Try this to see it is long or int Debug.Log("playerAvatar type" +playerProperties["playerAvatar"].GetType());

Comment
Add comment · 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

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

242 People are following this question.

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

Related Questions

PUN : RPCs or Stream? How reliable are RPCs? 1 Answer

Switching weapons in PhotonNetwork 1 Answer

Instantiate doesn't work with photonView.isMine? 0 Answers

How to get facebook friends using playfab? 1 Answer

Unity Online Not Work 2 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