Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NyeinChanAung · Mar 27 at 01:25 AM · timerfixedupdate

Is FixedUpdate() not called at the same time for all object?

In the following code there are three script ExpSpawner, ExpEnemy and ExpScore. The ExpSpawner spawn enemy and ExpEnemy move the enemy game object. At the start of the game the enemies will be spawned at every 2.5 sec and enemies also move at every 2.5 sec. During game play that timer will be reduced depends on the score. I'm counting timer in FixedUpdate() and compare it with a static variable that exist inside the ExpSpawner. Let's assume 30 enemies are spawned and moving every 2 seconds. And when the timer change a few numbers of enemies got wrong timer value and move at different speed but all the rest are doing well. If there's no timer changing feature or if I reset timer to zero ( not timer -= ExpSpawner.movSpawnTime; ), there is no such problem. And it doesn't happen everytime the game is played. It's very rare to happen. Didn't FixedUpdate() get the correct value of ExpSpawner.movSpawnTime whenever the time change or is the logic wrong?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ExpSpawner : MonoBehaviour
 {
     public static float movSpawnTime = 2.5f;
 
     public GameObject pfbEnemy;
     public int poolSz;
     public float initialXpos;
     public float initialYPos;
     public int spawnCount;
 
     private int spawnCounter;
     private float spawnTimer;
     private float reducer = 0.25f;
     private List<GameObject> pool;
 
     void Awake()
     {
         pool = new List<GameObject>();
         for (int i = 0; i < poolSz; i++)
         {
             GameObject obj = Instantiate(pfbEnemy);
             obj.SetActive(false);
             pool.Add(obj);
         }
     }
 
     void OnEnable()
     {
         ExpScore.LevelUp += OnLevelUp;
     }
 
     void FixedUpdate()
     {
         if (spawnCount != -1 && spawnCounter > spawnCount) return;
         if (spawnTimer < movSpawnTime)
         {
             spawnTimer += Time.fixedDeltaTime;
             return;
         }
         for (int i = 0; i < 8; i++)
         {
             GameObject obj = GetFromPool();
             obj.transform.position = new Vector3(initialXpos + i, initialYPos);
             obj.SetActive(true);
         }
         ++spawnCounter;
         spawnTimer -= movSpawnTime;
     }
 
     GameObject GetFromPool()
     {
         foreach (GameObject obj in pool)
         {
             if (!obj.activeInHierarchy) return obj;
         }
         GameObject enemy = Instantiate(pfbEnemy);
         enemy.SetActive(false);
         pool.Add(enemy);
         return enemy;
     }
 
     void OnDisable()
     {
         ExpScore.LevelUp -= OnLevelUp;
     }
 
     void OnLevelUp()
     {
         movSpawnTime -= reducer;
     }
 }  


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class ExpEnemy : MonoBehaviour
 {
     public static event Action EnemyLost;
     public float timer;
     // public float movSpTime;
     // public List<string> movSpList = new List<string>();
 
     public static int serial = 0;
     public Rigidbody2D rb2d;
 
     void Awake()
     {
         gameObject.name = "Enemy_" + ++serial;
     }
 
     void FixedUpdate()
     {
         if (timer < ExpSpawner.movSpawnTime)
         {
             timer += Time.fixedDeltaTime;
             return;
         }
         rb2d.MovePosition(rb2d.position + Vector2.down);
         timer -= ExpSpawner.movSpawnTime;
         // movSpTime = ExpSpawner.movSpawnTime;
         // movSpList.Add("Spawn time : " + ExpSpawner.movSpawnTime + " at " + Time.frameCount + ", timer = " + timer);
         // print("Reset time : " + timer);
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         gameObject.SetActive(false);
         EnemyLost?.Invoke();
     }
 
     void OnDisable()
     {
         timer = 0f;
     }
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class ExpScore : MonoBehaviour
 {
     public static event Action LevelUp;
 
     private int score;
     private int level;
     private int[] levelUpScore = { 10, 20, 30, 40, 50, 60 };
 
     void OnEnable()
     {
         ExpEnemy.EnemyLost += OnEnemyLost;
     }
 
     void OnDisable()
     {
         ExpEnemy.EnemyLost -= OnEnemyLost;
     }
 
     void OnEnemyLost()
     {
         ++score;
         if (level >= levelUpScore.Length) return;
         if (score >= levelUpScore[level])
         {
             ++level;
             LevelUp?.Invoke();
         }
     }
 }


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 rh_galaxy · Mar 27 at 03:18 AM

One thing you should do is to initialize variables before use.

 private float spawnTimer = 0.0f;
 private int spawnCounter = 0;
 ...
 private float timer = 0.0f;

Also it looks like you re-use objects, then you should be sure that you reset all variables. Not sure if OnDisable() is done on your enemies (When you Instantiate an enemy it is both deactivated then activated on the same frame)...

 void OnEnable()
 {
     spawnCounter = 0;
     timer = 0f;
 }
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

Is it possible to create a second FixedUpdate function running at different rate? 2 Answers

Making a Timer Out out of 3D Text using C#. 1 Answer

How can I store data from a timer? 1 Answer

Creating a Timer 0 Answers

more resource efficient TIMER 2 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