in javascript it can work with: Code (csharp): var myhit : RaycastHit; var myray : Ray; function Update () { myray = Camera.main.ScreenPointToRay (Input.mousePosition); if( Physics.Raycast(myray, myhit, 1000.0) Input.GetMouseButtonDown(0) ) print(myhit.collider.name); } but below doesn't work with "error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments": Code (csharp): using UnityEngine; using System.Collections; public class hitcs : MonoBehaviour { RaycastHit myhit = new RaycastHit(); Ray myray = new Ray(); void Update() { myray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(myray, myhit, 1000.0f) Input.GetMouseButtonDown(0)) print(myhit.collider.name); } } what did i miss??thx!i can't find unity manual for c#...
short : you need to add the "out" keyword before your RaycastHit parameter. long : the trick here is the keyword "out" in the method's signature in c#. as HitInfo is actually a c# struct and not a class, the default behavior when passing it to a method is to pass it by value instead of by reference. but in this case the method Raycast needs to modify the RaycastHit instance for you. that's why the signature of the method has a little "out" word, to specify that the structure must be passed by reference. hope it makes sense! oh and hello unity forum, this is my first post
Thanks ! That helped a lot ! Surely this info should appear here : http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
This is my Code (csharp): ~~first post as well.~~ ~ second post. Thank you very much for helping this finally make sense and work.