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 UniqueXDream94 · Jul 31, 2013 at 07:49 AM · timeontriggerenterfor-loophealth

[SOLVED] Need Help "For" loop to execute in specific Intervals

Hello,

I am fairly new to scripting, and am trying to learn as much as possible about the Unity engine. Currently, I am having trouble with the concept of the "For" loop. My goal is to make a game object: "Enemy1"'s hitPoint variable (set to 100) go down 1 number for every execution of the for loop, however, I need it to go down at timed intervals, not all at once. I know that I should use Time.deltaTime, but I am not sure how or where I should put it.. again I am new to scripting. Thank you SO much!

Here is the code currently attached to the Enemy1 object:

(I know its sloppy and contains many unnecessary variables and code. I created them while experimenting with code i've deleted most of the code unrelated to my question. If you need anything else or if i'm not being specific enough, just let me know)


 #pragma strict
 
 public static var myLog : String;
 public static var hitPoints :int = 100.0;
 var youWin : GameObject;
 public static var some_Name : String = "Enemy BOSS ";
 var boxHealth : GameObject;
 internal var forTime : float;
 
 
   
 function OnTriggerEnter (other: Collider) {
     if (other.tag == "Player"){
      Debug.Log("youve Entered my trigger");
      boxHealth.GetComponent(GUIText).enabled = true;

     //Here's where I am stumped.. I realize I probably messed up horribly..
     for (var i; i<100; i += forTime * Time.deltaTime){ 
      forTime = Time.time + forTime;
      hitPoints -=1; 
     }
     }
 }

 
 function OnTriggerExit (other: Collider){
 
     //boxHealth.SetActive (false);
     boxHealth.GetComponent(GUIText).enabled = false;
     Debug.Log("You've Exited my Trigger");
 }
 

 
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

1 Reply

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

Answer by DaveA · Jul 31, 2013 at 07:53 AM

Add this line in your for loop:

yield WaitForSeconds (1);

That will 'pause' the loop for 1 second allowing the rest of the program to run, then resume the loop. So it would take 100 seconds to complete.

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 UniqueXDream94 · Jul 31, 2013 at 07:58 AM 0
Share

haha wow, Thank you. worked Perfectly. To think it'd be so simple.

avatar image UniqueXDream94 · Jul 31, 2013 at 08:10 AM 0
Share

$$anonymous$$y Updated Code:

 function OnTriggerEnter (other: Collider) {
     if (other.tag == "Player"){
      Debug.Log("youve Entered my trigger");
      boxHealth.GetComponent(GUIText).enabled = true;
     
     for (var i=0; i<100; i++){
     yield WaitForSeconds (1);
      hitPoints -=1; 
     }

Again, Big thanks.

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

16 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

Related Questions

[Solved] Trying to pause a for loop function OnTriggerExit 1 Answer

Update not being called every frame 2 Answers

how to stop every thing and enter into the OnTriggerEnter function? 0 Answers

Health Regeneration 2 Answers

Triggering over time? 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