Search code examples
c#unity-game-engineunityscriptcode-conversion

How to convert this code from Unityscript to C#?


I am trying to convert Unity UnityScript to C#. I am getting this error:

Assets/Scripts/BoostPlatformCheck.cs(40,36): error CS0019: Operator ==' cannot be applied to operands of typeint' and `object'

Here is the C# code:

int collidedWith = collisionInfo.gameObject.GHetInstanceID();  
ArrayList collided = new ArrayList();

....

     if(collidedWith == collided[collided.Count - i - 1])
            {
                alreadyExists = true;
                return; 
            }

I changed Array to ArrayList and collided.length to collided.Count when I converted it from JS to C#.


Solution

  • I changed Array to ArrayList and collided.length to collided.Count when I converted it from JS to C#

    Since you just want to convert the code to C#, use array of int instead of ArrayList:

    int[] collided = new int [10]; // This is how you declare arrays in C#
    

    Then you can just use collided.length like in Javascript to find the length of the array.

    Also one more thing, arrays in C# has a built in function Contains() that may help you in your case, which seems like you're iterating over the array to compare collidedWith. To simplify, you can remove the loop and try this:

    // This means collidedWith exists within collided, so you won't need the loop anymore
    if(collided.Contains(collidedWith)) 
        alreadyExists = true;