Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 twoface262 · Jun 27, 2011 at 04:44 AM · syntax-errorbce0044

'If' Statement in javascript giving me problems

I'm trying to write a 'IF' statement for my health system heres the code I have.

 var healthmin = 0;
 var health = 100; 
 var GUIhealth : GUIText;
 var text = "Health: ";
 
 
 
 function Update(){
  if (healthmin == 1)
  {
   health--; 
   GUIhealth.text = text + health.ToString();
  }
  else
  {
   GUIhealth.text = text + health.ToString();
  } 
 
 
 function OnTriggerEnter( other : Collider ) { 
  if (other.tag == "zom") 
  {
   var healthmin = 1;
  } 
 }
 
 function OnTriggerExit( other : Collider ) {
  if (other.tag == "zom")
  {
   var healthmin = 0;
  }
 }

but its giving me that this error "Assets/playerhealth.js(21,10): BCE0044: expecting (, found 'OnTriggerEnter'." but I know there is nothing wrong with the 'OnTriggerEnter' Function because If i take the 'If' statement for the 'healthmin' on Function Update it works fine, There is something wrong with the Function Updates 'IF' Statement that I can't figure out, what am I doing wrong here?

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Jun 27, 2011 at 08:19 AM 0
Share

I re-formatted the code to use better indents, which should make the error more obvious to spot.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Macdude2 · Jun 27, 2011 at 04:53 AM

I think you are missing a parenthesis after the end of your Update() function. You cannot have an on trigger enter function inside your update function also.

 var healthmin = 0; 
 var health = 100; 
 var GUIhealth : GUIText; 
 var text = "Health: ";
 
 function Update(){ 
    if (healthmin == 1) { 
       health--; 
     GUIhealth.text = text + health.ToString(); 
 }
  else { 
      GUIhealth.text = text + health.ToString(); 
   }
 }
 
 function OnTriggerEnter( other : Collider ) {
  if (other.tag == "zom") { 
 var healthmin = 1;
 
       }
  }
 
 function OnTriggerExit( other : Collider ) {
 
  if (other.tag == "zom") {
  var healthmin = 0; 
      }
  }
Comment
Add comment · Show 2 · 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 twoface262 · Jun 27, 2011 at 04:56 AM 0
Share

Now the health isnt going down like it should :/

avatar image flaviusxvii · Jun 27, 2011 at 08:47 PM 0
Share

Yeah, inside your OnTrigger functions you want to ditch the "var" before "health$$anonymous$$". That's re-declaring the variable which will shadow the object variable. I posted an answer with a different way to look at it.

avatar image
0

Answer by Whimsical · Jun 27, 2011 at 04:50 AM

There's a } missing after

else { GUIhealth.text = text + health.ToString(); }

You're closing the else statement, but not the function. That's why the compiler is thrown off the path and isn't expecting OnTriggerEnter.

Please use proper code indentation. This helps you seeing these things a lot easier.

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
0

Answer by flaviusxvii · Jun 27, 2011 at 08:47 PM

 var thisHurts : bool = false; 
 var health:float = 100.0; 
 var GUIhealth : GUIText; 
 var text = "Health: ";

 function Update(){ 
     if (thisHurts) { 
         health -= Time.deltaTime; // Subtracting in this way will make the damage-rate frame rate independent 
     }
 
     GUIhealth.text = text + health.ToString(); // you were doing this no matter what..
 }

 function OnTriggerEnter( other : Collider ) {
     thisHurts = (other.tag == "zom");
 }

 function OnTriggerExit( other : Collider ) {
     thisHurts = !(other.tag == "zom");
 }
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with "Pick up key" script 2 Answers

Unity MoveAround error 1 Answer

Unity3d C to Javascript conversion? 1 Answer

(10,47): BCE0044: expecting :, found '='. 2 Answers

Formatting problems in Script 2 Answers


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