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 Daniel G · Jul 29, 2013 at 03:44 PM · javascriptfunction updateparticle animation

Breaking a loop, Javascript?

Hello, Im wanting to call a particle animation only once, it is in the function update. Would I do something like this:

 function Update () {
      if (gear = 1) {
           SomeParticleAnimation.Play();
           Break;
      }
      if (gear = 2) {
           SomeParticleAnimation.Play();
           Break;
      }
      //ect...
 }



I want to play an particle animation every time the gear changes.

Thanks Daniel

I take the time to accept and rate good answers!

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

3 Replies

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

Answer by SolidSnake · Jul 29, 2013 at 05:17 PM

As @Lovrenc said... Update() will keep on calling... Better to have a seperate function that controls the anims and which is called from update and which you can control how it is executed

You can do something like this:

 var lastGear:int;// stores the value of last gear
 var gear:int; // current gear
 function Update()
 {
   GearAnimation();
 }
 
 function GearAnimation()
 {
    if(lastGear == gear) // did the gear change? no then return to exit
       return;
 
    lastGear = gear;

    // then use switch or if statments to check the value (switch will suit this more)
    switch(lastGear)
    {
      case 1:
        SomeParticleAnimation.Play();
        break;
      case 2:
      // etc....
    }
 
 }

==EDIT==

What @Eric5h5 is saying is that instead of checking the gear value every frame (i.e. using the Update()) to play an animation once you should play the animation when the gear changes which what you will need to do eventually anyway.

As an example:

 // You will call this function only when the gear value need to change
 function ChangeGear(gearValue: int)
 {
    gear = gearValue;
 
    // play the corresponding animation for the current value ( using switch/ if statements)
    switch(gear)
    {
      case 1:
        SomeParticleAnimation.Play();
        break;
      case 2:
      // etc....
    }
 
    // do other things if any...etc
 
 }

   
Comment
Add comment · Show 8 · 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 Daniel G · Jul 29, 2013 at 08:17 PM 0
Share

So just to make sure break would cause the animation to ONLY be called once right? (@SolidSnake)

avatar image Lovrenc · Jul 29, 2013 at 08:30 PM 0
Share

The break itself only prevents switch statement to fall into another "condition". If there was no break in:

 case 1:
    SomeParticleAnimation.Play();
    break;

then (in case lastGear was 1) both statements under case 1: and under case 2: would execute.

But yes, as long as you will handle your lastGear variable correctly, your animation will only shoot once.

avatar image Eric5h5 · Jul 29, 2013 at 08:49 PM 0
Share

Unless you plan on calling that code from other functions, putting it in a separate function is just adding function call overhead while not changing anything.

avatar image SolidSnake · Jul 30, 2013 at 06:36 AM 0
Share

@Eric5h5 I based my answer on his question in regard to breaking an update and not on how the gear is changing which is separate question. Also, Having a separate function even though it will add some overhead it will make the code readable.

avatar image SolidSnake · Jul 30, 2013 at 06:43 AM 0
Share

@$$anonymous$$ G as @Lovrenc explained handling lastGear variable correctly will prevent the code from executing if gear didn't change value. But @Eric5h5 recommendation is valid if you don't need to check the gear value every frame. I simply based my answer thinking that gear is a variable you needed to check every frame since you used update function.

Show more comments
avatar image
1

Answer by Lovrenc · Jul 29, 2013 at 03:50 PM

Update is a function that is called within the loop and is not a loop itself.

You can exit the function by calling

 return;


But this seems like a bad practice and would probably produce error prone code. Just use switch on your

 gear

variable and do what is apropriate. This was your code is encapsulated and you can do other things that you may need in that function.

Switch is essentially same as if statements but is easier to read than a huge blob of ifs.

 switch(gear) {
   case 1:
     //Do your stuff
     break;
   case 2:
     //Do your stuff
     break
 }
Comment
Add comment · 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
1

Answer by Eric5h5 · Jul 29, 2013 at 08:53 PM

Don't use Update for this; it's for things that happen every single frame. Instead just run code when you actually need it. It depends on where you're actually changing the "gear" variable, but wherever that is, that's where you put the code.

 function SomeFunctionInWhichYouChangeGear () {
     if (gear == 1) {
         SomeParticleAnimation.Play();
     }
     ...
 }
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 Daniel G · Jul 30, 2013 at 02:46 PM 0
Share

Hmm, Im thinking, Seems like i could just read what the gear number is and execute the animation if that gear equals a specific number like you show, but I'm STILL lost as to WHERE i would put this "SomeFunctionInWhichYouChangeGear ();" In the update? Can you be specific :D @Eric5h5 If its in the update, would this cause it to NOT execute whats in the if statement (animation) here ever frame?

avatar image Eric5h5 · Jul 30, 2013 at 03:36 PM 1
Share

You would not use Update at all, that's the point.

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

Multiple Cars not working 1 Answer

How to access variable from another function? 2 Answers

How to make a selection system? 1 Answer

How to spawn objects in a specific range of random location 1 Answer

My Object only moves when the variable ready is true. Problem is that ready is only ever true while update is running. So to move the object the variable always has to be true. How can I make it move if the variable ready is false. 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