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 · Aug 01, 2013 at 01:58 AM · ontriggerenternewbiefor-loopontriggerexit

[Solved] Trying to pause a for loop function OnTriggerExit

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. Currently, I have an Enemy Object with a trigger radius and a "hitPoints" var attached to it set to 100. I have a for loop that OnTriggerEnter, the hitPoints variable goes down with every second.

The problem is, OnTriggerExit, I need that loop to pause and then resume again OnTriggerEnter at the same place it left off, but I don't know where to start... Can you help me please?

Also, (this is a secondary problem) I have a "destroyEnemy1()" function that if the variable hitPoints hit zero, the gameobject is destroyed, but its not executing properly. The Enemy1's "hitPoints" variable hits zero, then just sits there. If my "Player" object shoots bullets at it, the variable goes down into the negatives...


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)

 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 OnCollisionEnter(other: Collision) {

 if (other.gameObject.tag == "Bullet") {
     boxHealth.GetComponent(GUIText).enabled = true;
     hitPoints -=1;
 }
  
 

}

 function destroyEnemy1() {
 
 if (hitPoints <=0) {
     Destroy (gameObject);
     print("i'm Dead");
     
     GUI.enabled = false;
     Instantiate (youWin, transform.position, transform.rotation);
 }
 
     else{ 
         print("i'm still alive");
         
         //boxHealth.SetActive (true);
     }
 
 }
 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; 
     }
 }
 }
 
 function OnTriggerExit (other: Collider){
 
     //boxHealth.SetActive (false);
     boxHealth.GetComponent(GUIText).enabled = false;
     //pause for loop action script goes here
     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 · Aug 01, 2013 at 02:07 AM

You can declare a variable at the top of your script like:

var pause = false;

then in 'exit' set pause = true;

then in the for loop, just test for it.

   for (var i=0; i<100; ){
     if (!pause)
     {
     yield WaitForSeconds (1);
      hitPoints -=1; 
      i ++;
     }
     }

But it's getting convoluted. I personally would be using the Update function instead of a for loop.

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 UniqueXDream94 · Aug 01, 2013 at 02:52 AM 0
Share

hmm... The Unity engine keeps freezing each time my player exits the trigger area. Not sure if that's my computer acting up, or if there's a problem with the script..


Here's my updated script:

 public static var hitPoints :int = 100.0;
 var pause = false;
 
 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; ){
     if (!pause)
     {
     yield WaitForSeconds (1);
      hitPoints -=1; 
      i ++;
     }
     }
 
 }
 }
 
 function OnTriggerExit (other: Collider){
 
     //boxHealth.SetActive (false);
     boxHealth.GetComponent(GUIText).enabled = false;
     pause = true;
     Debug.Log("You've Exited my Trigger");
 }
 
avatar image Jamora · Aug 01, 2013 at 03:09 AM 0
Share

try

  for (var i=0; i<100 && !pause ; ){
 
 yield WaitForSeconds (1);
 hitPoints -=1;
 i ++;
 
 }

Right now, if pause is true, your indexer won't increase and you're stuck in an infinite for loop. Alternatively you could move i++; between the two curly backets;

avatar image UniqueXDream94 · Aug 01, 2013 at 03:30 AM 0
Share

Perfect! Thanks, its working now.

You guys have been a huge help :)

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

Turret Script Error 2 Answers

OnTriggerEnter function in c# 0 Answers

Make object react to certain triggers only 1 Answer

Box collider with strange behaviour 1 Answer

Help with OnTriggerEnter issue 3 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