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 /
avatar image
0
Question by Rowan_Edmondson · Sep 11, 2019 at 12:21 AM · 2dsynchronization2d-gameplaytimer countdowntimers

How do I synchronise different objects turning on and off?

Apologies in advance if this is moronically simple but I'm stuck in trying to synchronise different game objects turning off and on together on different timers.

I have a game obstacle (called a Death Wall) with a variable for the time it's 'on' (which kills the player on contact) and a time it's 'off'. My problem is keeping multiple instances of this object in sync with eachother when they're on different time spans. For example if one is set at 0.5 duration to be on and 0.5 duration to be off, and a second one is set at 1.0 on and 1.0 off, the second one will initially flick on and off perfectly in time with two cycles of the first, but over time it will lag behind and no longer be in sync.

Here's the current code:

 public class deathWallFinal : MonoBehaviour
 {
     public float startDelay;
     public float OnTime;
     public float OnTimer;
     public float OffTime;
     public float OffTimer;
     public float lifeCounter;
     public GameObject wall;
     public BoxCollider2D death;
     public GameObject DWG;
     public GameObject DWGtop;
     public GameObject particleLeft;
     public GameObject particleRight;
 
 
     // Start is called before the first frame update
     void Start()
     {
         
         death = GetComponent<BoxCollider2D>();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (startDelay > 0)
         {
             startDelay = startDelay - 1*Time.fixedDeltaTime;
             wall.GetComponent<SkeletonAnimation>().AnimationName = "off";
             death.enabled = false;
 
         }
 
 
         if (startDelay <= 0)
         {
             if (OnTimer > 0)
             {
                 OnTimer = OnTimer - 1 * Time.fixedDeltaTime;
                 OffTimer = OffTime;
                 death.enabled = true;
                 DWGtop.GetComponent<SkeletonAnimation>().AnimationName = "on";
                 DWG.GetComponent<SkeletonAnimation>().AnimationName = "on";
                 wall.GetComponent<SkeletonAnimation>().AnimationName = "on";
                 particleLeft.SetActive(true);
                 particleRight.SetActive(true);
 
 
             }
             if (OnTimer <= 0)
             {
                 OffTimer = OffTimer - 1 * Time.fixedDeltaTime;
 
                 death.enabled = false;
                 DWGtop.GetComponent<SkeletonAnimation>().AnimationName = "off";
                 DWG.GetComponent<SkeletonAnimation>().AnimationName = "off";
                 wall.GetComponent<SkeletonAnimation>().AnimationName = "off";
                 particleLeft.SetActive(false);
                 particleRight.SetActive(false);
             }
             if (OffTimer <=0)
             {
                 OnTimer = OnTime;
             }
         }
   

Sorry if this is an annoying question or isn't well articulated. Any help would be greatly appreciated :)

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

Answer by DrawMen1999 · Sep 12, 2019 at 05:36 PM

You could use a coroutine to just waitForSeconds before turning it on/off, their timing should be good.

 void Update(){
    On();
    Waiting(onTime);
    Off();
    Waiting(offTime);
 }
 
 IEnumerator Waiting(float waitTime)
 {
    yield return new WaitForSeconds(waitTime);
 }
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 Rowan_Edmondson · Sep 16, 2019 at 02:17 AM 0
Share

No luck unfortunately. I changed things over to coroutines but the same issue persists. If one is on for 1 second and off for 1 second, and a second obstacle is on for 2 seconds then off for 2 seconds, after a few cycles they're out of sync again

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

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

OnTriggerEnter2D Running twice when destroying game object. 1 Answer

Instantiated objects do not work with Hitboxes(2D) 0 Answers

Draw Gizmos for Non-Monobehaviour objects in an Array 0 Answers

moving player top down,movement in unity 2d 0 Answers

How to realize FreeRiderHD-like game mechanic? 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