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

4
  • 1
    Try: if(collidedWith ==Convert.ToInt32(collided[collided.Count - i - 1])) Commented Aug 20, 2014 at 1:23
  • 5
    What is stored in your ArrayList? If they're all the same thing - which it appears they are; ints - then you should be using a List<int>.. which will allow your comparison. Also, don't do what Tim said to do above this comment. Commented Aug 20, 2014 at 1: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. Commented Aug 20, 2014 at 1:31
  • also it's UnityScript, not JavaScript Commented Aug 20, 2014 at 7:50

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.