- Home /
How to "Camera.ScreenPointToRay". I get an error in MonoDevelop.
Hello, I am pretty new to Unity. I am trying to cast a ray from the main camera, mouse position into the game world. This video http://www.youtube.com/watch?v=P0PHY1hJp5k shows exactly what I want to do. I have also tried to copy and paste from the Unity script reference page. I thought the unity script reference was supposed to be a working code snippet. When I try either one and to debug them I get the same 3 error messages. The 2 versions of code are..
void Update () {
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay (ray.origen, ray.position * 10, Color.cyan);
}
and
public class ExampleClass : MonoBehaviour {
void Update() {
Ray ray = camera.ScreenPointToRay(new Vector3(200, 200, 0));
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
}
This one is from the Unity script reference. When I try to debug them I get these 3 errors and warning...
Error CS0029: Cannot implicitly convert type 'UnityEngine.Ray' to 'Ray' (CS0029) (Assembly-CSharp).
Error CS1061: 'Ray' does not contain a definition for 'origin' and no extension method 'origin' accepting a first argument of type 'Ray' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp).
Error CS1061: 'Ray' does not contain a definition for 'direction' and no extension method 'direction' accepting a first argument of type 'Ray' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp).
Warning CS0618: 'UnityEngine.Camera.mainCamera' is obsolete: 'use Camera.main instead.' (CS0618) (Assembly-CSharp).
So I understand the Warning. I have Unity version 4.1.5f1 on my other computer and all this code works fine. Running the newest version, how can I make the code work now?
Answer by robertbu · Jun 08, 2014 at 03:52 PM
You code looks fine. The problem is that somewhere in your project you have created a Ray script or a Ray class. This class is being used when you declare a 'Ray' rather than the Unity Ray class. Rename your class, and the problem will go away.
Wow! Thank you this was it. The only thing that I noticed that didn't work was the difference between "ray.position and ray.direction.". The "ray.direction" was the only one that worked in the end. Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613151058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
cant kill more than one enemy C# 2 Answers
Raycast doesn't collide as it should 1 Answer
enemy detect player then attack - c# 1 Answer
NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers
Line Renderer not showing 0 Answers