Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GrKl · Aug 24, 2013 at 01:10 PM · c#synccooldown

seperate the cooldown from multiple enemies

Hi all,

I am struggeling on what seems to be a simple problem. I have a classic cooldown function (c#) to delay the attacks of my enemies. This works fine.

But if multiple enemies are present in range, they seem to all sync their cooldown, I'dd like them of course to each have their own cooldown.

Enemies are prefabs, so they have the same scipts attaches to them

my actual simple cooldown attached to each enemy:

 private float attackTimerRange;
 private float coolDownRange;
 
 void Start(){
     attackTimerRange = 0;
     coolDownRange = 5.0f;
 }
 
 void Update(){
     if (attackTimerRange > 0)
     {
         attackTimerRange -= Time.deltaTime;
     }
     if (attackTimerRange < 0)
     {
         attackTimerRange = 0;
     }
 }

Thanks for any help possible

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 YoungDeveloper · Aug 24, 2013 at 01:21 PM 0
Share

Hi, each enemy should have their own cool down script, create one universal script for an enemy, and attach it to all enemy's. And I'd suggest writing all cool down related stuff inside separate function, or at least most of it.

avatar image meat5000 ♦ · Aug 24, 2013 at 01:29 PM 0
Share
  if (attackTimerRange > 0)
 {
 attackTimerRange -= Time.deltaTime;
 }
 if (attackTimerRange < 0)
 {
 attackTimerRange = 0;
 }

Where do you update attackTimerRange? This says it is pretty much stuck on 0, right?

avatar image GrKl · Aug 24, 2013 at 01:30 PM 0
Share

they do actually. The above is just a part of their attack script, all enemy have this script attached to them. But it seems that the "attackTimerRange" even if it is private, is shared by all of them. This is what I dont understand and would like to prevent

avatar image meat5000 ♦ · Aug 24, 2013 at 01:32 PM 0
Share

I get that :P I'm asking where :D post it

avatar image Jamora · Aug 24, 2013 at 01:47 PM 0
Share

In addition to meat5000's observation of attackTimerRange being stuck at 0, it is also running on each enemy from the point of instantiation ins$$anonymous$$d of when it's shooting.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Aug 24, 2013 at 03:53 PM

Firstly, change the class to a non-public class.

If all your enemies are present at game start and not instantiated their timers will all start at the same time.

As

 attackTimerRange = 0;

and

 if (attackTimerRange == 0)
 {
 attackTimerRange = coolDownRange;

seem to be the only things that modify this, they will always be in sync.

Try something like:

 void Start()
 {
     attackTimerRange = Random.Range(0,11);
 }

To add a random element to each Enemy.

Also consider using float instead of int if you want a larger degree of difference

Comment
Add comment · Show 6 · 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 GrKl · Aug 24, 2013 at 04:01 PM 0
Share

actually, their timer are set back to coolDown (wich is 5f) each time they attack; this works, but it seems that when 1 attacks me, they are all waiting 5seconds before next attack. So its seems the timer is shared.

avatar image meat5000 ♦ · Aug 24, 2013 at 04:03 PM 0
Share

lol try the suggestion first. I did acknowledge the coolDown in the answer :) Also, is there any particular reason why the class is public? A public class could be the cause of your issue and the scope of the variables is very wide compared to a non-public class in which the variables are not seen from outside.

avatar image GrKl · Aug 24, 2013 at 04:14 PM 0
Share

I'm quite new on Unity and C#, but it seems the class can not be set as private.

changing the Timer time to be a random one would not solve the problem.

Enemy attacks when the attackTimerRAnge is = 0; when they do attack, this timer is set to 5f. When the timer is above 0, it loses 1f per second. thus, after having attacked, the enemy is forced to wait 5 seconds before next attack.

avatar image meat5000 ♦ · Aug 24, 2013 at 04:17 PM 0
Share

The randomiser at the start serves only to stagger the times across the enemies so they hit 0 at different times. Even if they are all set to 5 this will occur at different times also. Ya get me?

Just take out the word public and see what happens If a variable has scope that extends outside its class or function that same variable is accessible by other classes or functions. As all your enemies share the same variable names AND the class is public this could cause your issue.

avatar image GrKl · Aug 24, 2013 at 04:41 PM 0
Share

indeed! changing the attackTimerRange to a range (float ideally) solves it. I am still unbit unsure why this works, but it does!

thanks a lot

Show more comments

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Syncing times 0 Answers

How do i get an ad cooldowner? 0 Answers

Networking Sync SetActive Not Working 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