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 GodOfTheFools · Dec 05, 2013 at 06:52 PM · updatetimefunctionscorefunction update

How to call a function only once in Update

Hi guys, the problem I'm having within my script is that I need to be able to detect when a particle emitter should activate and when it does remove a certain amount of points from the score.

Now, it does activate the emitter and also DOES remove points from the score, the problem is that due to it having to be within the "function Update" it's removing points every frame which results in the score being drastically reduced.

What I want to know is if there's anyway I can make it so that it will only be called the one time or if there's a possible workaround for the problem.

Code is below:

 var Beep : AudioClip;
 public var wheelFL : WheelCollider;
 public var fire : ParticleEmitter;
 public var explosion : ParticleEmitter;
 private var lastMovementTime : float = Mathf.Infinity;
 
 function Start()
 {
     fire.emit = false;
     lastMovementTime = Time.time;
 }
     
 function Update ()
 {
 
     if (wheelFL.rpm > 0.2)
     {
         lastMovementTime = Time.time;
     } 
     else 
     {
         if ((Time.time - lastMovementTime) > 5)
         {
             audio.PlayOneShot(Beep);
             //flash_headlights();
         }
                 
         if ((Time.time - lastMovementTime) > 9) 
         {
             onFire();
         }
     }
     
     if ((fire.emit == true) && (Time.time - lastMovementTime > 19))
         {
             explode();
         }
 }
 
 function onFire()
 {
     fire.emit = true;
     StatisticsScript.scorePoints -= 5;
 }
             
 function explode()
 
 {
     explosion.emit = true;
     yield WaitForSeconds (3.0);
     Destroy (gameObject);
     StatisticsScript.scorePoints -= 10;
 }
 

Thanks guys, any help will be greatly appreciated.

Comment
Add comment · Show 1
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 meat5000 ♦ · Dec 05, 2013 at 11:27 PM 0
Share
 if ((fire.emit == true) && (Time.time - last$$anonymous$$ovementTime > 19))
 {
      last$$anonymous$$ovementTime = Time.time;
      explode();
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by stevethorne · Dec 05, 2013 at 07:48 PM

You probably don't want to have the fire.emit as true when it explodes, so you could turn that off in the explode function. This will stop explode from being called multiple times. Your explode function would look like this:

 function explode()
 {
     fire.emit = false;
     explosion.emit = true;
     yield WaitForSeconds( 3.0 );
     Destroy( gameObject );
     StatisticsScript.scorePoints -= 10;
 }

Alternatively you could add another bool in your code that keeps track of whether or not your object has exploded. Then your if statement could read like so:

 if ( ( fire.emit == true ) && ( Time.time - lastMovementTime > 19 ) && !exploded )

In your explode function you can set exploded to true. And wherever you spawn the object you can set exploded to false.

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 GodOfTheFools · Dec 05, 2013 at 10:11 PM 0
Share

Although useful, this doesn't actually answer the question I've posed as it would still be removing points every frame while the fire particle emitter is still active.

avatar image stevethorne · Dec 05, 2013 at 11:48 PM 0
Share

Then you could use a boolean to keep track of whether or not you have removed the points, then you can set it to true when it removes the points, and set it back to false whenever you want to be able to remove points again.

So your code would have this if statement now:

 if ( !hasRemovedPoints )
 {
     StatisticsScript.scorePoints -= 10;
     hasRemovedPoints = true;
 }

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

18 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 avatar image

Related Questions

Fixed timestep... 1 Answer

Is It Bad To Have Your Jump In Update()? 3 Answers

Execute code every x seconds with Update() 4 Answers

Can you redefine functions with names like Update(), etc.? 2 Answers

Update increment error (2 + 1 = 0?) 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