Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RaulDiaz · Oct 15, 2010 at 08:33 PM · guibce0044mmorpgswitch-case

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Nov 04, 2010 at 01:40 AM 0
Share

That's not correct. Anything after case is executed up until break;.

avatar image Mondeo · Nov 04, 2010 at 03:59 AM 0
Share

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.

avatar image Mondeo · Nov 04, 2010 at 02:01 PM 0
Share

To be fair - you can have multiple statements after case, but you can't organize them in curly brackets.

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RaulDiaz · Oct 15, 2010 at 08:46 PM 0
Share

EDIT* i added the semicolons to the print features and it is still giving me the same error.

avatar image Grumblekeen · Oct 15, 2010 at 08:49 PM 0
Share

Did you read everything I wrote? What about the missing end bracket?

avatar image RaulDiaz · Oct 15, 2010 at 08:49 PM 0
Share

I checked and did not find a missing } bracket.

avatar image Grumblekeen · Oct 15, 2010 at 08:59 PM 0
Share

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

avatar image RaulDiaz · Oct 15, 2010 at 09:03 PM 0
Share

Fixed the missing bracket and indention but still receiving the same error any ideas?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Question regarding transporting my character to new level using switch case 2 Answers

Error in my script i don't understand. 0 Answers

Unity for a 2D MMORPG and GUI 1 Answer

Trying to get an array to make working buttons. [Script Update] 2 Answers

Trying to Destroy Object when Wave starts 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges