- Home /
Unexpected Errors - Need help & explaination plz
Part of a tutorial I'm following has this code in the script -
if(Physics.Raycast(r, out hit)
{
while(!passables.Contans(hit.transform.gameObject.name))
{
if(!Physics.Raycast(hit.point + r.direction*0.1f, r.direction, out hit)
{
break;
}
}
}
However, I'm getting these two errors -
"Assets/Scripts/CameraOperator2.js(71,43): BCE0044: expecting ), found 'hit'."
"Assets/Scripts/CameraOperator2.js(75,100): BCE0044: expecting ), found 'hit'."
Any ideas/suggestions ???
I don't typically code in js, but shouldn't that be passables.Contains not Contans? Also you need to close the ) at the end of the first line.
Turns out you are actually missing two parenthesis. One at the end of 43 and one at the end of 47:
if(Physics.Raycast(r, out hit))
{
while(!passables.Contains(hit.transform.gameObject.name))
{
if(!Physics.Raycast(hit.point + r.direction*0.1f, r.direction, out hit))
{
break;
}
}
}
Answer by HarshadK · Feb 06, 2015 at 01:16 PM
The problem is that there is no need to use 'out' keyword in front of hit in JS (required in C#).
So raycast line becomes:
if(Physics.Raycast(r, hit))
and
if(!Physics.Raycast(hit.point + r.direction*0.1f, r.direction, hit))
Now solving this will yield some other errors which are also solved here in this code below:
#pragma strict
public var selectionHighLight : Texture2D = null;
public static var selection : Rect = new Rect(0, 0, 0, 0);
private var startClick : Vector3 = -Vector3.one;
private static var moveToDestination : Vector3 = Vector3.zero;
private static var passables : String = ""; //{"Floor"};
function Update()
{
CheckCamera();
CleanUp();
}
function CheckCamera()
{
if(Input.GetMouseButtonDown(0))
{
startClick = Input.mousePosition;
}
else
if(Input.GetMouseButtonUp(0))
{
if(selection.width <0)
{
selection.x += selection.width;
selection.width = -selection.width;
}
if(selection.height <0)
{
selection.y += selection.height;
selection.height = -selection.height;
}
startClick = -Vector3.one;
}
if(Input.GetMouseButton(0))
{
selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));
}
}
function OnGUI()
{
if(startClick != -Vector3.one)
{
GUI.color = new Color(1, 1, 1, 0.2);
GUI.DrawTexture(selection, selectionHighLight);
}
}
static function InvertMouseY(y : float)
{
return Screen.height - y;
}
function CleanUp()
{
if(!Input.GetMouseButtonUp(1))
{
moveToDestination = Vector3.zero;
}
}
public static function GetDestination(argumentVariableName : Vector3)
{
if(moveToDestination == Vector3.zero)
{
var hit : RaycastHit;
var r : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r, hit))
{
while(!passables.Contains(hit.transform.gameObject.name))
{
if(!Physics.Raycast(hit.point + r.direction*0.1f, r.direction, hit))
{
break;
}
}
}
if(hit.transform != null)
{
moveToDestination = hit.point;
}
return moveToDestination;
}
}
Some of the things that are changed here are:
1) Changed declaration of string
private static var passables : String = ""; // This is how you initialize an empty string
2) Changed HierarchyType.point to hit.point since there is no 'point' type under HierarchyTpe and I think what you are looking for is point of Raycast Hit.
3) You need to provide a name to the variable that will be passed to your method as I've added below:
public static function GetDestination(argumentVariableName : Vector3)
@Harshad$$anonymous$$ many(repeat infinitely) thanks. I truly appreciate your help.
However I do have a couple of questions - The line you changed to read
private static var passables : String = ""; // This is how you initialize an empty string
Was (in the original tutorial) written as-
private static List<string> passables = new List<string>() {"Floor"};
Typically, they added an update tutorial later that says the name "Floor" has to be in the script for it to work.
Does this mean I have to add the word 'Floor' into my String = " " or do I need to somehow add it afterwards (similar to how the original line was written ?
The update tutorial- https://www.youtube.com/watch?v=m-oU2mSZykA
Secondly, you said I need to 'provide a name to the variable that will be passed to your method.'
Why do I need to do that? (please don't take that as I don't believe you, I just want to learn/understand exactly 'why' it is I need to do it)
thanks
The statement from the tutorial
private static List<string> passables = new List<string>() {"Floor"};
is for declaring a collection of type string with floor as an element.
And you pass a variable name so that the value passed when that function is called can be accessed through that variable, which you can manipulate in the function definition.
Answer by VesuvianPrime · Feb 05, 2015 at 07:18 PM
Missing a close bracket here:
yeah, I thought extra brackets were needed, but when I tried that I got these additional messages
"Assets/Scripts/CameraOperator2.js(71,47): BCE0043: Unexpected token: )."
"Assets/Scripts/CameraOperator2.js(75,104): BCE0043: Unexpected token: )."
???
If you're getting unexpected tokens it probably means you're missing brackets or semi-colons somewhere. Usually on the line prior to where you're getting the error.
here's the script in full -
#pragma strict
public var selectionHighLight : Texture2D = null;
public static var selection : Rect = new Rect(0, 0, 0, 0);
private var startClick : Vector3 = -Vector3.one;
private static var moveToDestination : Vector3 = Vector3.zero;
private static var passables : String = new String(); //{"Floor"};
function Update()
{
CheckCamera();
CleanUp();
}
function CheckCamera()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
startClick = Input.mousePosition;
}
else
if(Input.Get$$anonymous$$ouseButtonUp(0))
{
if(selection.width <0)
{
selection.x += selection.width;
selection.width = -selection.width;
}
if(selection.height <0)
{
selection.y += selection.height;
selection.height = -selection.height;
}
startClick = -Vector3.one;
}
if(Input.Get$$anonymous$$ouseButton(0))
{
selection = new Rect(startClick.x, Invert$$anonymous$$ouseY(startClick.y), Input.mousePosition.x - startClick.x, Invert$$anonymous$$ouseY(Input.mousePosition.y) - Invert$$anonymous$$ouseY(startClick.y));
}
}
function OnGUI()
{
if(startClick != -Vector3.one)
{
GUI.color = new Color(1, 1, 1, 0.2);
GUI.DrawTexture(selection, selectionHighLight);
}
}
static function Invert$$anonymous$$ouseY(y : float)
{
return Screen.height - y;
}
function CleanUp()
{
if(!Input.Get$$anonymous$$ouseButtonUp(1))
{
moveToDestination = Vector3.zero;
}
}
public static function GetDestination(Vector3)
{
if(moveToDestination == Vector3.zero)
{
var hit : RaycastHit;
var r : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r, out hit))
{
while(!passables.Contains(hit.transform.gameObject.name))
{
if(!Physics.Raycast(hit.point + r.direction*0.1f, r.direction, out hit))
{
break;
}
}
}
if(hit.transform != null)
{
moveToDestination = HierarchyType.point;
}
return moveToDestination;
}
}
$$anonymous$$aybe seeing the whole script will help put things in context
Well... my head is about to explode. If I don't put in the extra brackets it says ) is missing, if I do put them in it says ) is an unexpected token.
Surely one of you guys have some ideas as to what is wrong here?
Your answer
Follow this Question
Related Questions
What is wrong with these two lines of scripting? 1 Answer
Internal compiler error 2 Answers
Compiler errors while switching to android 1 Answer
Unity Compiler Error Glitched. 0 Answers
Problem with Mono compiler 0 Answers