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 Necromancer134 · Aug 01, 2013 at 05:57 AM · not working

Unity If statement issue (It goes through the function without meeting the parameters for the if statements)

Okay so I have the basics down but I'm still new to programming with unity. I'm creating a simple fps and I'm at the point where I'm doing my enemy scripting. I thought I created a script that made sense for the enemy health and when the enemy dies but it's not working correctly.

 var enemyHealth = 50;
 
 function Start(){
     enemyHealth = 50;
     print("Why is this messed up?");
     }
 
 function OnCollisionEnter(collider : Collision) {
     
     if(collider.gameObject.tag == "Bullet");
         enemyHealth -= 10;
     };

//Heres my issue

 function EnemyDie(){
     if (enemyHealth == 0);
         Destroy(gameObject);
         print("Function Worked!");
         }
 
 function Update(){
     EnemyDie();
     }`

So this keeps destroying itself, The EnemyDie() function is where it happens. If I comment that out, the script runs fine but doesn't destroy itself after. The other function works as it is supposed too, the enemy doesn't take damage unless it gets hit with a bullet. I've tried many variations and cannot figure this out, please help. Thank you.

Note * I made an even simpler script to test out the concept of my code and still had the same issue.

 var Health = 50;
 
 function Start () {
     Health = 50;
 }
 
 function Update () {
     if (Health <= 0);
         print("Hello World!");
 }

The parameter to print "Hello World" is that Health has to be less than or equal to 0. Health is at 50 and there's no way in this function to lose health, and yet somehow, it prints "Hello World!"

I'm so confused.

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

2 Replies

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

Answer by tw1st3d · Aug 01, 2013 at 06:02 AM

Just gonna go ahead and fix these from start to finish.

 var enemyHealth = 50;
 
 function Start()
 {
     enemyHealth = 50;
     print("Why is this messed up?");
 }
 
 function OnCollisionEnter(collider : Collision)
 {
     if(collider.gameObject.tag == "Bullet")
         enemyHealth -= 10;
 }
 
 function EnemyDie()
 {
     if (enemyHealth <= 0)
     {    
         Destroy(gameObject);
         print("Function Worked!");
     }
 }
 
 function Update()
 {
     EnemyDie();
 }
 
 
 var Health = 50;
  
 function Start()
 {
     Health = 50;
 }
  
 function Update () 
 {
     if (Health >= 0)
         print("Hello World!");
 }

Basically, if statements don't need ; at the end of the line, because it's a conditional, not a procedure.

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 Necromancer134 · Aug 01, 2013 at 06:18 AM 0
Share

Okay, makes sense. When I take away the ";" from the collision function, it stops working, but when I take away ";" from the EnemyDie() function, it works the way I need it, so thank you.

avatar image tw1st3d · Aug 01, 2013 at 04:32 PM 0
Share

What you're thinking of with doing }; is ending a C# class, javascript wouldn't use that for a function. No idea why that would cause your function to not work.

avatar image Necromancer134 · Aug 01, 2013 at 04:37 PM 0
Share

You're guess is better than $$anonymous$$e, so I don't know. But regardless, everything is working, so thank you for the help! :D

avatar image
0

Answer by dorpeleg · Aug 01, 2013 at 06:04 AM

You are not writing the if statements correctly.

When doing 1 line under an if stament you can write it like this:

 function Update () {
     if (Health <= 0)
        print("Hello World!");
 }

*notice there is no ';' after the if.

If you are doing more then 1 line under the if statement, you should write it like this:

 function EnemyDie(){
     if (enemyHealth == 0){
        Destroy(gameObject);
        print("Function Worked!");
    }
 }

*notice there is no ';' after the if and the added '{}'.

This is basic programming, not only in unity.

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 Necromancer134 · Aug 01, 2013 at 06:19 AM 0
Share

I took a program$$anonymous$$g class and attempted to $$anonymous$$ch myself, somewhere I received bad information and was never corrected. I took away the ";" and it broke one function but fixed the other, simple solution just keep it for the one I need updated and remove it for the EnemyDie(), thank you for the help!

avatar image dorpeleg · Aug 01, 2013 at 06:21 AM 0
Share

If this helped you, please mark the as answered.

(you can mark both of the answers if you like :) )

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

17 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Help with gun scripting!? 1 Answer

I have a Multiplayer script here 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Inup.GetButtonDown not working? 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