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
1
Question by bubble1 · Apr 13, 2015 at 10:56 PM · javascriptupdatefunctionwork

Update function doesn't work correctly

Hi there. I'm developing a simple game of zombie survival. This script is attached in an empty GameObject, which acts as the player's health.

 #pragma strict
 
 var health = 100;
 var attack = 10;
 var otherAttackDelay = 5;
 
 function Attack1 () {
 health -= attack;
 Debug.log ("Under Attack 1!");
 yield WaitForSeconds (otherAttackDelay);
 Attack2 ();
 }
 
 function Attack2 () {
 health -= attack;
 Debug.log ("Under Attack 2!");
 }
 
 function Update () {
 if (health == 80) {
 Debug.log ("Die!");
 }
 }

The player has 100 health, and attack a zombie is 10. There are 5 second delay before the zombie attack again.

In the function Update I wrote a condition, if the player's health is equal to 80 (2 attacks) will display a Debug log with the message "Die!"

The problem is that when the zombie attacks only once (90 of life would there) immediately shows the "Die!". What am I doing wrong?

Greetings.

Comment
Add comment · Show 3
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 MajdHamada · Apr 13, 2015 at 11:23 PM 1
Share

the problem is that your health is -= attack every frame you have to make var like this ..

var Attacked :boolean = false;

 function Attack1 () {
 if( attacked == false){
  health -= attack;
 }
 attacked = true
  Debug.log ("Under Attack 1!");
  yield WaitForSeconds (otherAttackDelay);
 attack = false;
  Attack2 ();
 
  }

 
 
avatar image hexagonius · Apr 14, 2015 at 06:21 AM 0
Share

Where are you calling`yield Attack1();`

avatar image LeonardNS · Apr 17, 2015 at 01:50 PM 0
Share

If you call Attack1 multiple times from another script it could screw your system over. Also since Attack1 calls the Attack2 function which both takes 10 HP you are going to die. After your program is finished with Attack1 it will proceed to Attack2 because of the last line(11).

1 Reply

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

Answer by ticofa · Apr 14, 2015 at 11:22 PM

Hi :)! I'm not very familiar with java scripting, but i'd assume that its similiar to C# on the fact that, if you create costume functions, you will need to call them in the Update function :P

For example:

 function Attack1 () {
  if( attacked == false){
   health -= attack;
  }
  attacked = true
   Debug.log ("Under Attack 1!");
   yield WaitForSeconds (otherAttackDelay);
  attack = false;
   Attack2 ();
  
   }
 
 
 function Update()
 {
 Attack1();
 }




Hope it helps ;)!

Comment
Add comment · Show 1 · 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 bubble1 · Apr 19, 2015 at 10:36 AM 1
Share

mh92 and you solved the problem, thank you! (:

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

7 People are following this question.

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

Related Questions

How to crouch without falling through floor? 2 Answers

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

Script is causing immense lag, and I don't know what's causing it. 2 Answers

touch buttons function OnGUI issue 1 Answer

Static function and variables error 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