- Home /
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;
}
}
}
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"] );
@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;
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.
@Anox I am do not understand what you meen but there is two sprite in avatars
So what is the value you recieve from: player.CustomProperties["playerAvatar"] ?
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());
Your answer
Follow this Question
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