Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>