- Home /
InvalidCastException: Specified cast is not valid. (NETWORKING)
I am making a multiplayer FPS game and I am getting the error in the title.
Here is the whole error:
InvalidCastException: Specified cast is not valid. GameManager.UpdatePlayers_R(System.Object[] data) (which is the script and function down below)
public void UpdatePlayers_R(object[] data)
{
playerInfo = new List<PlayerInfo>();
for (int i = 0; i < data.Length; i++)
{
object[] extract = (object[])data[i];
PlayerInfo p = new PlayerInfo // marks the error here
(
new ProfileData
(
(string)extract[0],
(int) extract[1],
(int) extract[2]
),
(int) extract[3],
(short) extract[4],
(short) extract[5]
);
playerInfo.Add(p);
if (PhotonNetwork.LocalPlayer.ActorNumber == p.actor) myind = i - 1;
}
}
Answer by Bunny83 · Apr 30, 2020 at 07:30 PM
Well it's pretty obvious that at least one of the values in your nested "extract" array is not what you think it is. So either the first element is not a string, element 1,2 or 3 is not an int or element 4 or 5 is not a short. Since you used an object initializer we can not tell you which one it might be. You could try splitting the casts into seperate lines with local variables in order to figure out which one it is:
var p0 = (string)extract[0];
var p1 = (int) extract[1];
var p2 = (int) extract[2];
var p3 = (int) extract[3];
var p4 = (short) extract[4];
var p5 = (short) extract[5];
PlayerInfo p = new PlayerInfo
(
new ProfileData ( p0, p1,p2 ),
p3,
p4,
p5
);
Now it should point you to the exact line where the cast fails. Once you know which element it is you can print its actual type. Just place this loop before you do any casting and it will print the types of all values.
for(int i = 0; i extract.Length; i++)
{
if (extract[i] == null)
Debug.Log("extract["+i+"] is null");
else
Debug.Log("extract["+i+"] type == " + extract[i].GetType().Name);
}
Your answer
Follow this Question
Related Questions
Using lerp to smooth network movement causes rubber banding 1 Answer
Photon - Hide/Show Object 1 Answer
Unity Photon Ready check 0 Answers
Updating multiplayer map on joining 1 Answer
Photon timer issue 2 Answers