- Home /
InvalidCastException with .net datatypes but not with UnityEngine.Object
The following returns the following error: Line 11: InvalidCastException: Cannot cast from source type to destination type.
It works perfectly with anything that inherits from UnityEngine.Object. I don't understand how to prevent this from happening with .net datatypes.
void Start () {
float[] f = new float[5];
f[0] = 4.5f;
Test(f);
}
void Test(object t)
{
if (t is Array)
{
Debug.Log("isarray");
object[] a = (object[])t;
if (a[0] is float)
{
Debug.Log("isfloat");
}
}
}
Answer by Sisso · May 01, 2013 at 12:19 PM
Correct, float didn't extends object.
You need te take a read about basic types, arrays and casting. Until you didn't understand it is better to use a more complex collection like ArrayList. (link)
Edited
I found the oficial docs about the problem, if you wish to get a deep understand
Your answer
Follow this Question
Related Questions
Problem with type casting from C# to JS 3 Answers
Cannot cast from source type to destination type 0 Answers
How do I cast an IntPtr to another class? 0 Answers
Converting to ColliderCast - what's included in the PhysicsWorld and how do I include my own meshes? 0 Answers
Animator is a field not a type? 1 Answer