Conversion From Unity JS to C# - Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
I'm trying to convert Unity JS
function LoadLastSavedSelection() { var strIDs : String[] = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));
var ids : int[] = new int[strIDs.Length];
for(var i = 0; i < strIDs.Length; i++)
ids[i] = parseInt(strIDs[i]);
Selection.instanceIDs = ids;
}
into C#. And below my last converted that still have some errors.
void LoadLastSavedSelection (){ string[] strIDs = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));
int[] ids = new int[strIDs.Length];
for(float i = 0; i < strIDs.Length; i++)
ids[i] = int.Parse(strIDs[i]);
Selection.instanceIDs = ids;
}
Errors code that I get
(308,43): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
(308,21): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
Line 308 is
ids[i] = int.Parse(strIDs[i]);
Can anyone help me solve the conversion so I don't have some errors. Thank in advance!
Answer by Wibbs 1 · Apr 14, 2011 at 11:30 AM
its because i is declared as a float in the for loop. Array indexes need to be integers, so the line should read:
for(int i = 0; i < strIDs.Length; i++)
Yes, my fault! I have some floats o other code before that make me forget about 'culprit' float at current code. Great help, Thank!
Your answer
![](https://koobas.hobune.stream/wayback/20220613122749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
problem with dff importer script 1 Answer
2D minimap - convert click point to world point 0 Answers
Java to C# Translation 1 Answer
How to make a character controller through another script 0 Answers