0

I'm building a game in Unity3d using C#. I was looking at some code, however it is in unityscript. I came this code and I'm not sure how to convert it to C#:

for(var hit : Collider in colliders)
3
  • 2
    That is not Java. Commented Oct 17, 2015 at 23:20
  • Yeah, wondering that language actually is ... sure not java. Commented Oct 17, 2015 at 23:23
  • 3
    Without knowing UnityScript or even that much C#, I would assume this will end up being a foreach statement. Commented Oct 17, 2015 at 23:33

1 Answer 1

4

well that's fairly easy to write in C# You're using a for-each loop and C# has a specific foreach keyword for this situation. Your code becomes the following :

foreach(var hit in colliders)

The thing you might not get from this is this: why is he not specifying that hit is of type Collider? Well, in C#, when using the keyword var, you're actually declaring that this variable is anonymous and the compiler will use type inference to determine what type of data is actually being passed around.

Sign up to request clarification or add additional context in comments.

1 Comment

Hum @Frog If you like the answer, please upvote it and click on the mark symbol to show that my answer was helpful ! In the future, people with the same question will understand quickly that it's the right answer for this question

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.