- Home /
C# out hit for raycasthit - why is the out needed, what does it go
I notice that RaycastHit is called differently for JS vs C#... In C#, the hit is actually out hit
... what does the out do, and why is it needed?
Answer by rutter · Apr 07, 2012 at 11:07 PM
Most function arguments are passed by value -- that is, you pass a copy of the original value, rather than the variable itself. Changes made to that copy won't be reflected in the original variable unless your code does so explicitly.
Some functions accept arguments that are passed by reference -- instead of a copy, you pass the original variable itself. Changes made in the called function will be reflected in the original variable.
C# and UnityScript both typically pass by value, unless the called function explicitly tells the compiler to pass by reference. C# is unique in that it requires coders to explicitly pass by reference on both sides of the call -- I assume the idea is to make sure that you know you're doing so, as it can sometimes lead to confusion.
Ultimately, both languages are doing the same thing. C# just has a slightly different syntax for doing so.
what if I don't add the out before the hit? are there any effect?
Sure, you will get an error. Out forces a variable to be passed by reference, the difference between using out or ref is that ref requires the variable to be initialized first, out doesn't(meaning it can be null or whatever the default state of the variable is).
what should i initialize if i use ref?All of the variables in hit?
Answer by kyon77 · Apr 08, 2012 at 12:20 AM
As far as I know, the Out gives you the info about what it hit, like the distance to the hit, the name of the object it hit, the point where it hit, and so on.
It can be very helpful in some cases where you wanna know if a laser hit a friendly target or an enemy, for example.
Your answer
Follow this Question
Related Questions
How can i use raycast hit to detect object by the object tag name ? 1 Answer
C# change an object tag wit raycasthit. 2 Answers
Raycast executing hundreds of times 2 Answers
Another Raycast Issue. 0 Answers