Jump to content

Invalid Cast Exception


Recommended Posts

Hey,

I recently ran into a problem with 'Invalid Cast Exception - Cannot cast from source to destination type'.

I cannot locate where the error is, cause my log full of things looking like this:


InvalidCastException: Cannot cast from source type to destination type.
at MusicPlayer.WindowGUI (Int32 windowID) [0x00000] in <filename unknown>:0

at UnityEngine.GUILayout+LayoutedWindow.DoWindow (Int32 windowID) [0x00000] in <filename unknown>:0

at UnityEngine.GUI+_Window.Do () [0x00000] in <filename unknown>:0

at UnityEngine.GUI.EndWindows (UnityEngine.IDList idlist) [0x00000] in <filename unknown>:0

at UnityEngine.GUIUtility.EndGUI (Int32 doLayout, Int32 doWindows, UnityEngine.IDList idlist) [0x00000] in <filename unknown>:0

(Filename: Line: -1)

Is it something with my plugin\'s GUI?

I add my GUI via

RenderingManager.AddToPostDrawQueue(3, new Callback(drawGUI));

First, I removed all namespaces, still no avail.

The weird thing is, when adding some print() commands to drawGUI() it shows me

that the error is happening after drawGUI has finished?

drawGUI looks like this:

  private void drawGUI()
{
GUI.skin = HighLogic.Skin;
if (guiMode == 'exp')
{
windowPos = GUILayout.Window(1, windowPos, WindowGUI, 'Music Player', GUILayout.MinWidth(200));
}
else if (guiMode == 'mini')
{
windowPos = GUILayout.Window(1, windowPos, WindowGUI, 'Music Player', GUILayout.MinWidth(200), GUILayout.MaxHeight(100));
}
}

And WindowGUI:


private void WindowGUI(int windowID)
{
GUIStyle mySty = new GUIStyle(GUI.skin.button);
mySty.normal.textColor = mySty.focused.textColor = Color.white;
mySty.hover.textColor = mySty.active.textColor = Color.yellow;
mySty.onNormal.textColor = mySty.onFocused.textColor = mySty.onHover.textColor = mySty.onActive.textColor = Color.green;
mySty.padding = new RectOffset(8, 8, 8, 8);
mySty.fixedWidth = 200;

GUIStyle scrollSty = new GUIStyle(GUI.skin.scrollView);


GUILayout.BeginVertical();

if (guiMode == 'exp')
{
if (GUILayout.Button('Minimize', GUILayout.ExpandWidth(true)))
{
guiMode = 'mini';
}
}
else if (guiMode == 'mini')
{
if (GUILayout.Button('Expand', GUILayout.ExpandWidth(true)))
{
guiMode = 'exp';
}
}

GUILayout.Label((string)musiclib[curSongPos], GUILayout.ExpandWidth(true));

GUILayout.BeginHorizontal();
if (GUILayout.Button('Play',GUILayout.Width(mySty.fixedWidth / 3)))
{
player.Play();
}
if (GUILayout.Button('Pause',GUILayout.Width(mySty.fixedWidth / 3)))
{
player.Pause();
}
if (GUILayout.Button('Stop',GUILayout.Width(mySty.fixedWidth / 3)))
{
player.Stop();
}
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
if(GUILayout.Button('Prev', GUILayout.Width(mySty.fixedWidth / 2))){
prevSong();
}
if (GUILayout.Button('Next', GUILayout.Width(mySty.fixedWidth / 2)))
{
nextSong();
}
GUILayout.EndHorizontal();

player.volume = GUILayout.HorizontalSlider(player.volume, 0.01f, 1.0f, GUILayout.ExpandWidth(true));

if (guiMode == 'exp')
{
GUILayout.BeginScrollView(new Vector2(0, 0), GUILayout.ExpandWidth(true), GUILayout.Height(200));
int ti = 0;
foreach (MusicPath actpath in musiclib)
{
if (GUILayout.Button(actpath.showname, GUILayout.ExpandWidth(true)))
{
selectSong(ti);
}
ti+=1;
}
GUILayout.EndScrollView();
}

GUILayout.EndVertical();
GUI.DragWindow(new Rect(0, 0, 10000, 20));

}

These are pretty much the only functions that are called all the time.

Any Ideas? I\'m pretty much stuck :/

Thanks,

Naelo

Link to comment
Share on other sites

I think the exception is being thrown here:

GUILayout.Label((string)musiclib[curSongPos], GUILayout.ExpandWidth(true)); 

musiclib[curSongPos] seems to return as int32 and you are trying to cast it into a string.

Edit: so to explain a little further. Invalid Cast Exception can only happen when you cast something i.e.

Part p = (Part)Object.Initialize(something);

The section you have provided from your output log is also the place it is being reported, albeit poorly at that. It is just a trace of the error, where the top line tells you exactly where the exception occurred, in your case at MusicPlayer.WindowGUI (Int32 windowID) [0x00000] in <filename unknown>:0 . So you know it happened in your WindowGUI method and it has something to do with Int32. Unfortunately the filename references are useless here.

Link to comment
Share on other sites

I agree with Romfarer, only 2 things to add:

1. If

musiclib[curSongPos]

does resolve to an int, and you want to display it, might want to try removing the (string) cast, and making it

musiclib[curSongPos].tostring()

2. If you\'re still getting problems, try adding a few

Debug.Log('Made it this far: 0');

lines inside whatever block is causing the problems, that\'s helped me figure out exactly where the problem occurs many times debugging my own code. The last log to show up before you hit the exception in the place to start looking.

Link to comment
Share on other sites

Yes - that was the problem!

I used to only store strings in that ArrayList but then... I changed it to a custom class which has two attributes.

Trying to convert the class to a string obviously doesnt work.

Thanks for the help, I would never have spotted that.

Link to comment
Share on other sites

Actually, you CAN convert a class to a string, that\'s why I mentioned .tostring().

If you\'re using VS, just type override then space, and select object.tostring(). Or write the override yourself. Useful if you want a data-container object to be 'string-able' for debug printing/etc.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...