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 EstragonHelmer · Feb 12, 2019 at 03:06 PM · timerepeating

How do I synchronise a function called in multiple objects?

Example - I need a bunch of objects to all call a specific function in their script every 5 seconds. I'm thinking using InvokeRepeating.

If all those objects are present at the start of the scene, they start in sync, so they stay that way.

If I create new objects at different times in the game, they'll be out of sync with each other.

What's the best way to make them all call that function at the same time every time?

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
2
Best Answer

Answer by Hellium · Feb 12, 2019 at 03:49 PM

I suggest you using a dedicated class to handle the synchronisation, I've called it Repeater. Attach it to an empty gameObject for instance.

 using UnityEngine;
 
 public class Repeater : MonoBehaviour
 {
     [SerializeField]
     private float interval;
 
     public UnityEngine.Events.UnityEvent OnRepeat;
 
     private float timer;
     
     public float Interval
     {
         get { return interval ;}
         set { interval = Mathf.Max( value, 0 ); }
     }

     private void Start()
     {
          timer = Interval;
     }
   
     private void Update()
     {
         timer -= Time.deltaTime ;
         if( timer < 0 )
         {
             timer += interval ;
             if( OnRepeat != null )
                 OnRepeat.Invoke();
         }
     }
 }



Then, depending on how you manage your instances, you will have to make those instances subscribe to the OnRepeat event. Here here an example with 2 simple scripts : a Spawner and a Spawnee

 // Spawnee.cs    
 public class Spawnee : MonoBehaviour
 {
     public void SayMyName()
     {
         Debug.Log( name );
     }
 }


 // Spawner.cs
 public class Spawner : MonoBehaviour
 {
     // Drag & drop the gameObject holding the Repeater script in the inspector
     public Repeater Repeater;
 
     private void Spawn()
     {
         Spawnee instance = Instantiate( spawneePrefab ) ;
         Repeater.OnRepeat.AddListener( instance.SayMyName ) ;
     }
 }
 
Comment
Add comment · Show 1 · 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 EstragonHelmer · Feb 12, 2019 at 04:30 PM 0
Share

Thanks for this! I've not used event systems before, so this was super helpful.

I altered it a bit, so the spawnee's prefab adds the listener to itself when it's created, so both spawned ones and those in the scene when it starts both have it. Totally working now!

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

165 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 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

How can I compare transform.position now and transform.position after 2 seconds? 2 Answers

few buttondown in fixed time 1 Answer

Suspension of Physics 1 Answer

adding time to int 0 Answers

how to change the animation lengh by script? 0 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