简体   繁体   中英

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 type int' 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#.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM