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 _= parseInt(strIDs*);*_
 _*Selection.instanceIDs = ids;*_
_*}*_
_*```*_
_*<p>into C#. And below my last converted that still have some errors.</p>*_
_*```*_
 _*void LoadLastSavedSelection (){*_
 _*string[] strIDs = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));*_
 _*int[] ids = new int[strIDs.Length];*_
 _*for(float i = 0; i < strIDs.Length; i++)*_
 <em><em>ids _= int.Parse(strIDs*);*_</em></em>
 <em><em>_*Selection.instanceIDs = ids;*_</em></em>
 <em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>
<em><em>_*<p>Errors code that I get</p>*_</em></em>
<em><em>_*<blockquote>*_</em></em>
 <em><em>_*<p>(308,43): error CS0266: Cannot implicitly convert type `float' to`int'. An explicit conversion exists (are you missing a cast?)</p>*_</em></em>
 
 <em><em>_*<p>(308,21): error CS0266: Cannot implicitly convert type `float' to`int'. An explicit conversion exists (are you missing a cast?)</p>*_</em></em>
<em><em>_*</blockquote>*_</em></em>
<em><em>_*<p>Line 308 is</p>*_</em></em>
<em><em>_*```*_</em></em>
 <em><em><em><em>ids _= int.Parse(strIDs*);*_</em></em></em></em>
<em><em><em><em>_*```*_</em></em></em></em>
<em><em><em><em>_*<p>Can anyone help me solve the conversion so I don't have some errors. Thank in advance!</p>*_</em></em></em></em>

2 Answers

2

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!

http://www.m2h.nl/files/js_to_c.php

Thanks for the great resources.