0

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#.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
LooMeenin
  • 818
  • 3
  • 17
  • 33
  • 1
    Try: `if(collidedWith ==Convert.ToInt32(collided[collided.Count - i - 1]))` – Tim.Tang Aug 20 '14 at 01:23
  • 5
    What is stored in your `ArrayList`? If they're all the same thing - which it appears they are; `int`s - then you should be using a `List`.. which will allow your comparison. Also, **don't do what Tim said to do above this comment**. – Simon Whitehead Aug 20 '14 at 01:23
  • 4
    I am shocked that Tim's comment has an upvote - its 2014 people - we have strongly typed containers for this. You shouldn't be casting objects from an `ArrayList` from .NET 2 and onward. Certainly not in this simple instance anyway. – Simon Whitehead Aug 20 '14 at 01:31
  • also it's UnityScript, not JavaScript – CodeSmile Aug 20 '14 at 07:50

1 Answers1

1

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;
Jay Kazama
  • 3,167
  • 2
  • 19
  • 25