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 Gustavo-Santos · Apr 02, 2019 at 12:00 AM · editorexecution-order

Really strange behaviour on really simple script (Creating Ticking System)

I'm creating a ticks system for unity, and I'm breaking my head with something super weird in a super simple code.

1 - What do I want? Create a system that calls updates on other systems defined by a number of updates per second, even if it can cause lag in case the number of updates per second is very large.

2 - Based on this I created 2 scripts (Tickator.cs and Tickated.cs), in which the Tickator will be responsible for calculating how many ticks have passed to update the systems that are tickable.

 //Tickator.cs
 using UnityEngine;
 [DefaultExecutionOrder(0)]  
 public class Tickator : MonoBehaviour  
 {  
     [Range(0, 240)] public uint TicksPerSecond;  
       
     public int TotalTicks;  
     public Tickated TickatedClass;  
       
     private float _t;  
   
     private void Update()  
     {  
         _t += Time.deltaTime * TicksPerSecond;  
         int elapsedTicks = (int) _t;  
   
         if (elapsedTicks > 0)  
         {  
             for (int i = 0; i < elapsedTicks; i++)  
             {  
                 DoTick();  
             }  
               
             _t %= 1;  
         }  
     }  
   
     private void LateUpdate()  
     {  
         if (TotalTicks != TickatedClass.Ticks)  
         {  
             Debug.LogError("count of ticks on Tickator is different from count of ticks in Tickated!");  
             Debug.Break();  
         }  
     }  
   
     private void DoTick()  
     {  
         TotalTicks++;  
         TickatedClass.Tick();  
     }  
 }


 //Tickated.cs
 using UnityEngine;  
 public class Tickated : MonoBehaviour  
 {  
     public int Ticks;  
   
     public void Tick()  
     {  
         Ticks++;  
     }  
 }


3 - I added 2 debug variables one in each script to have control of how many ticks were ticked. And a Debug.Break () that will pause the editor and throw an error when the ticks between Tickator and Tickated are different (they should never be).

4 - I put both in a gameobject and assign TIckated to the field TickatedClass

5 - If I hit play everything works perfectly, the amount of ticks between ticker and ticked never changes (perfect).

6 - While in play mode, if I hit pause, then change the value of TicksPerSecond and return to play, the amount of ticks between tickator and tickated never changes (also perfect).

7 - But if I change this value while in playmode soon the amount of ticks between tickator and tickated changes (Whyyy)!

8 - What is more strange is, I created a class that changed the TicksPerSecond value of the Tickator that I called Setter.cs, when I change the value through it, updating the TicksPerSecond value of the Tickator through the MonoBehaviour Update event, the error does not occur. The same happens if I change the value through the game by Sliders or InputFields.

 //Setter.cs
 using UnityEngine;  
 [DefaultExecutionOrder(-1000)]  
 public class Setter : MonoBehaviour  
 {  
     [Range(0, 240)] public uint Value;  
     public Tickator m_Tickator;  
       
     private void Update()  
     {  
         m_Tickator.TicksPerSecond = Value;  
     }  
 }


9 - I also tried changing the order of execution between Tickator.cs and Setter.cs using DefaultExecutionOrder, to see if the same error happened, but it did not happen ...



Git Repository or Unity Package Zip



Can someone help me understand what's going on? Thanks in advance,

tickingsystem.zip (3.4 kB)
Comment
Add comment · Show 3
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 Quelandoris · Apr 02, 2019 at 12:14 AM 0
Share

I don't think I'm understanding what you're trying to do. It sounds like what you're trying to make is a delegate event that gets called by a timer.

avatar image Gustavo-Santos Quelandoris · Apr 02, 2019 at 12:21 AM 0
Share

I'm trying to make a system that updates other systems, in which I can set the amount of updates per second.

avatar image toddisarockstar · Apr 02, 2019 at 05:43 AM 0
Share

i see you are using delta time in your script... this is a time mesurement. when you make your cast from float to int after using deltatime you totally kill the accuracy. delta time is typically a number from zero to one and when you cast it it will round down to the nearest whole number.

so anyways if you say you are only using your tick system to be in scync with ever changing frames per second than you should be using all whole numbers or ints in update.

if you are concerned about timers relating to accurate time then you should be using delta time or some other timed c# carotene.

you cant mix both. thats why everyone is asking what and why you are doing what you are doing. most times in game development it is standard to use time ins$$anonymous$$d of FPS for triggers because FPS changes depending on the device. using frames is just as possible but we are asking what you are doing because we are not sure what you are doing is the right apporoach. one way or another your code looks very long just to be organizing different triggers

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hameed-ullah-jan · Apr 02, 2019 at 05:57 AM

Update function works with Frames/Seconds, if you want a time controlled kind of update function, you can simply use fixed update and you can control that time from the Fixed timesteps (Edit->Project settings->Time), by default fixed update is called every 0.02 seconds, you can change this value.

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
0

Answer by toddisarockstar · Apr 02, 2019 at 06:05 AM

     public int[] frameCounters;
     public int[] counts;
     void Start () {
         frameCounters = new int[] {204,824,2456,10000,10202};
         counts = new int[frameCounters.Length]; 
 
     }
     
     // Update is called once per frame
 // Change to FixedUpdate for time counting
     void Update () {
         int i = counts.Length;
         while(i>0){i--;
             counts[i]++;
             if(counts[i]>=frameCounters[i]){
                 print("counter number:"+ i+ " has reached:"+counts[i]+" frames");
                 counts[i]=0;
             }    }}
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
0

Answer by Gustavo-Santos · Apr 02, 2019 at 09:57 AM

The thing is, how is it possible that the count of ticks in the Tickator could be different from the count of ticks in Tickated? Because both are iterated in the same method...

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

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

137 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

Related Questions

Reset Animaton Editor 0 Answers

Editor.DestroyImmediate crashes Unity. Am I using it wrong? 3 Answers

Unity Execution Order kept at export? 1 Answer

How to include multiple unity projects into one main unity project? 1 Answer

Inspector turns black on refresh 4 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