expecting } found Else and expecting EOF found } ???
so this code throw out both the errors specified above and i cant find any reason for it. the code it supposed to check if the ray hit something and then return what it hit or that it didnt hit anything. it is all inside the update function.
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.green);
If (Input.GetMouseButtonDown (0)) {
If (Physics.Raycast (ray, hit)) {
print (" I am looking at " + hit.transform.name);
}
else {
Print ("I am looking at nothing!");
}
}
That's the whole script? Is this meant to be C# or JS? I've never seen any language in which "if" has a capital "I".
Answer by dmm_jbw · Apr 15, 2016 at 03:58 PM
I'm not sure if your code doesn't compile because it's a mix of different languages, or you just typed it up with some problems. This compiles for me:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.green);
if (Input.GetMouseButtonDown (0))
{
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
Debug.Log(" I am looking at " + hit.transform.name);
}
else
{
Debug.Log("I am looking at nothing!");
}
}
At this point I'm going to see if something is wrong with my unity overall because i couldn't get that to run either.
This code isn't supposed to run unless it's inside a .cs (C#) file and inside a class and a function...
just making sure because in these kinds of questions you can't take that for granted without more details since there's incomplete code in the original question
Your answer
![](https://koobas.hobune.stream/wayback/20220612074906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Storing Renderer in a variable not working! 1 Answer
Raycasting else not working properly 0 Answers