- Home /
Case Statement Error?
So im working on a single player MMORPG (yes i know mmorpg stands for massively multiplayer online.....etc) im just not experienced in making anything multiplayer yet.
Back on topic im getting this error
"Assets/My Assets/scripts/closest.js(43,61): BCE0044: expecting :, found '='."
with this code
//debug text var tree : String;
//Menu Settings
var MenuActive1:boolean = false;
var timer : float = 5;
//Find closest Tree/Click on tree?
function Update () {
//Finding Closest Tree
var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("ClickAble");
var closest: GameObject;
var closestDist = Mathf.Infinity;
for (waypoint in waypoints) {
var dist = (transform.position - waypoint.transform.position).sqrMagnitude;
tree = waypoint.name;
print(Object);
print(dist);
if (dist < closestDist) {
closestDist = dist;
closest = waypoint;
}
}
//LookAt Closest
//transform.LookAt(closest.transform);
///////////////////////////////////////
//Click Code
if (Input.GetMouseButton(0)) {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
var playerDist = transform.position;
var distance = dist;
var obHit = hit.transform;
if(distance < 8 && timer == 5 ){
timer = 0;
if (Physics.Raycast(ray, hit)) {
switch ( obHit.name ){
case HTree001:
{
MenuActive1 = true;
print(hit.transform.name);
}
case HTree002:
{
MenuActive1 = true;
print(hit.transform.name);
}
}
}
}
else {timer += 1;}
}
}
function OnGUI () {
if (MenuActive1 == true)
{
if (MenuActive1 == true){
// Make a background box
GUI.Box (Rect ((Screen.width-31)/2,190,220,180), "Large Tree\n\n \n .\n\n .");
//Second button returns to main menu
if (GUI.Button (Rect((Screen.width-31+200)/2,285,80,20), "Accept")) {
MenuActive1=!MenuActive1;
}
//Second button returns to main menu
if (GUI.Button (Rect((Screen.width-31+200)/2,320,80,20), "Cancel")) {
//Closes the menu
MenuActive1=!MenuActive1;
}
}
}
}
With this line
MenuActive1 = true;
EDIT* i added the semicolons to the print features and it is still giving me the same error.
Answer by skovacs1 · Oct 15, 2010 at 10:19 PM
What a mess! I don't see any missing braces as Grumblekeen describes, but there is no lack for things wrong here, including inconsistent indentation, abuse of switch statements, needless and unused variables, incorrect type usage, inconsistent type indications, premature variable declarations, use of unassigned variables, attempt to use a variable that exists in a different scope and generally poor form:
//debug text var tree : String; //Do you even use this?
//Menu Settings var MenuActive1 : boolean = false; var timer : float = 5.0f;
function Update () { //Finding Closest Tree var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("ClickAble"); var closest: GameObject; var closestDist = Mathf.Infinity;
for (var waypoint : GameObject in waypoints) {
var dist : float = (transform.position -
waypoint.transform.position).sqrMagnitude;
tree = waypoint.name;
//print(Object); //You're printing a type?
//I think you mean
print(waypoint.name);
print(dist);
if (dist < closestDist) {
closestDist = dist;
closest = waypoint;
}
}
//LookAt Closest
//transform.LookAt(closest.transform);
///////////////////////////////////////
//Click Code
if (Input.GetMouseButton(0)) {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit; //Why declare this so early?
var playerDist : Vector3 = transform.position; //This unused var isn't a distance
//var distance : float = dist; //why no just use dist since it isn't changed?
///Wait a tic! dist doesn't exist in this context. Did you mean closestDist?
//var obHit : Transform = hit.transform; //No. This is bad. hit is null.
if(closestDist < 8 && timer == 5 ){
timer = 0;
if (Physics.Raycast(ray, hit)) {//Now hit is something.
//This is just bad.
//obHit was null.
//Unless HTree001, etc. are strings, this won't work
//You never break out of your cases so they fall through.
//If this is intentional, why not just use an if?
//Weren't obHit and hit.transform supposed to be the same things?
//Why switch usage part way through?
//switch ( obHit.name ){
// case HTree001:
// {
// MenuActive1 = true;
// print(hit.transform.name);
// }
// case HTree002:
// {
// MenuActive1 = true;
// print(hit.transform.name);
// }
//}
var hitName : String = hit.transform.name;
switch(hitName) {
case "HTree001" :
case "HTree002" : {
MenuActive1 = true;
print(hitName);
}
}
}
}
else timer += 1;
}
}
function OnGUI () { if (MenuActive1 == true) {
//if (MenuActive1 == true){ //twice? // Make a background box GUI.Box (Rect ((Screen.width-31)/2,190,220,180), "Large Tree\n\n \n .\n\n ."); //Second button returns to main menu if (GUI.Button (Rect((Screen.width-31+200)/2,285,80,20), "Accept")) { MenuActive1=!MenuActive1; } //Aren't these two pretty much the same? //Second button returns to main menu if (GUI.Button (Rect((Screen.width-31+200)/2,320,80,20), "Cancel")) { //Closes the menu MenuActive1=!MenuActive1; } //} } }
Here's a cleaner version (I changed some spacing because I hate horizontal scroll):
//Menu Settings var MenuActive1 : boolean = false; var timer : float = 5.0f;
function Update () { //Finding Closest Tree var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("ClickAble"); var closest: GameObject; var closestDist = Mathf.Infinity; for (var waypoint : GameObject in waypoints) { var dist : float = (transform.position - waypoint.transform.position).sqrMagnitude;
print(waypoint.name); print(dist); if (dist < closestDist) { closestDist = dist; closest = waypoint; } } //LookAt Closest //transform.LookAt(closest.transform); /////////////////////////////////////// //Click Code if (Input.GetMouseButton(0)) { if(closestDist < 8 && timer == 5 ){ var hit: RaycastHit; timer = 0; var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, hit)) { var hitName : String = hit.transform.name; if(hitName == "HTree001" || hitName == "HTree002") { MenuActive1 = true; print(hitName); } } } else timer += 1.0f; } }
function OnGUI () { if (MenuActive1 == true) {
// Make a background box GUI.Box (Rect ((Screen.width-31)/2,190,220,180), "Large Tree\n\n \n .\n\n ."); //Second button returns to main menu if (GUI.Button (Rect((Screen.width-31+200)/2,285,80,20), "Accept")) { MenuActive1=!MenuActive1; }
//Second button returns to main menu
if (GUI.Button (Rect((Screen.width-31+200)/2,320,80,20), "Cancel")) {
//Closes the menu
MenuActive1=!MenuActive1;
}
}
}
There are so many more improvements and logical oversights to be touched upon, but this should at least resolve any compilation errors in this section of this script.
Answer by Mondeo · Nov 04, 2010 at 01:01 AM
I've got a similar problem. After some experimentation it seems that after case you can write only one statement. You can't group a few in curly brackets Don't know why though.
That's not correct. Anything after case is executed up until break;
.
Wanna bet on it? var fn : Number; var sh : boolean; function Update(){ switch(fn){ case 1 : { sh = false; } break; default : break; } } This will give an error. Remove the curly brackets after the case and it will work.
To be fair - you can have multiple statements after case, but you can't organize them in curly brackets.
Answer by Grumblekeen · Oct 15, 2010 at 08:42 PM
You are missing a } for this if "if (Physics.Raycast(ray, hit)) {" I think.
I strongly recommend you keep your blocks indented to make it easier to visually see things like this.
You are also missing semicolons on your print statements within the switch.
Typically, if the complier is throwing an error that doesn't seem to make sense for a specific line, it is confused by a missing "something" earlier in your code, such as a bracket, semicolon, paren, etc.
EDIT* i added the semicolons to the print features and it is still giving me the same error.
Did you read everything I wrote? What about the missing end bracket?
You are definitely missing a bracket. I just checked it in unitron. $$anonymous$$ove your cursor around the bracket that is meant to close the update function and you'll see it is actually closing "if (Input.Get$$anonymous$$ouseButton(0)) {". Set up your indents and you'll see it
Fixed the missing bracket and indention but still receiving the same error any ideas?