Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Rodolinc · Jul 23, 2018 at 09:41 AM · eventsdelegates

Delegate events inside Update

Hi I usually come up with this "problem" and want to know if there's a better way to do it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example: MonoBehaviour {
 
     public delegate void ExampleEvents();
     public static event ExampleEvents E_JustAnEvent; 
 
     // Use this for initialization
     void Start () {
         
     }
 
     // Update is called once per frame
     void Update () {
 
         if (animator.GetCurrentAnimatorStateInfo(0).IsName(walkState))
         {
 
             if (E_JustAnEvent!= null)
                 E_JustAnEvent();
 
             Debug.Log("event multiple times");
 
         }
     }
      
 }
 

The problem is that of course the event gets triggered each frame, so I usually fix this by having a flag variable so it gets triggered only the first time.

It's ok when you have only one or even two events in the Update but the moment you have more, it becomes a hassle.

Also I usually don't like to use too much the Update function but sometimes is needed like in this case checking the animation state.

What do you suggest?

(I don't like though, using in Editor solutions for example adding an animation event set in the Editor)

Thanks!

Comment
Add comment · Show 7
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 NoDumbQuestion · Jul 23, 2018 at 10:21 AM 0
Share

Did you know you can add script to animation state?

avatar image Rodolinc NoDumbQuestion · Jul 23, 2018 at 10:37 AM 1
Share

Yes, but that's why I mentioned in my question that I don't want to use in editor solutions (only because it's more manageable in the company I'm working atm)

avatar image JVene Rodolinc · Jul 23, 2018 at 11:50 AM 1
Share

Your point brings up the more general wisdom that if an issue is fixed by a configuration of the editor, the problem is actually still left in the code and isn't, therefore, portable to usage elsewhere. It is not just the company your working in, but the act of working with others at all, or where code may ever be exported to other projects, or made a public product for other programmers. It is better to fix the code so the problem no longer exists, and never has to be fixed by configuration of the editor.

Show more comments
avatar image Rodolinc NoDumbQuestion · Jul 23, 2018 at 10:38 AM 0
Share

Also the question doesn't have to do with animation, but with ways to trigger events only once in the Update function

avatar image NoDumbQuestion · Jul 23, 2018 at 10:42 AM 0
Share

You can add event for animation clip at run time. That way, it will only call the event when animator run/go through it.

Get the animation clip inside your animator GameObject in awake. Then add an event(time 0.1f) for it.

avatar image Rodolinc NoDumbQuestion · Jul 23, 2018 at 11:01 AM 0
Share

nice idea about adding anim events at runtime

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Question regarding delegates and events 1 Answer

Different approach to an upgrade system 4 Answers

When should i be using events 1 Answer

Instance variables and this == null in event handler 1 Answer

HoloLens Callbacks with Native Library 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