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 /
This question was closed Mar 21, 2020 at 11:02 AM by Vinnihu for the following reason:

Problem is solved

avatar image
0
Question by Vinnihu · Mar 20, 2020 at 08:09 PM · 2d gametimerobjectstimer countdown

Instantiate gets called every frame and doesn't wait for timer

Hey, I want to Instantiate a bullet every 2 seconds or so but it gets called every frame even though I set an interval. What's wrong with my code?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Tower : MonoBehaviour
 {
     GameObject[] enemyList;
     float attackRange = 5f;
     GameObject nearestEnemy;
     GameObject bulletPrefab;
     float attackInterval = 2f;
     float nextAttack = 0f;
 
     void Start()
     {
         bulletPrefab = Resources.Load<GameObject>("Prefabs/BulletPrefab");
     }
 
     void Update()
     {
         findNearestEnemy();
         if (nextAttack <= 0)
         {
             nextAttack = attackInterval;
             if (nearestEnemy != null)
             {
                 if (Vector2.Distance(nearestEnemy.transform.position, transform.position) <= attackRange)
                 {
                     Instantiate(bulletPrefab, transform.position, Quaternion.identity);
                     nearestEnemy = null;
                 }
             }
         }
         else
         {
             nextAttack -= Time.deltaTime;
         }
     }
 
     void findNearestEnemy()
     {
         enemyList = GameObject.FindGameObjectsWithTag("Enemy");
         if (enemyList.Length != 0)
         {    
             float minDist = Mathf.Infinity;
             foreach (var enemy in enemyList)
             {
                 float dist = Vector2.Distance(transform.position, enemy.transform.position);
                 if (dist < minDist)
                 {
                     nearestEnemy = enemy;
                 }
             }
         }
     }
 }
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

  • Sort: 
avatar image
1

Answer by ShadyProductions · Mar 20, 2020 at 08:15 PM

1). You never actually set or reset your nextAttack it is always <= 0, after instantiate reset it to 2f

2). You have a lot of inefficiency in your method. (calling FindGameObjectsWithTag every frame)

You should consider caching your enemies.

Comment
Add comment · Show 21 · 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 Vinnihu · Mar 20, 2020 at 08:18 PM 0
Share

What do you mean with caching the enemies?

avatar image BBIT-SOLUTIONS Vinnihu · Mar 20, 2020 at 08:25 PM 1
Share

With caching he mean that you could create a List or an Array, in which you save references to your Enemies each time when you create them. In this way it is easier just to get access to all of them, e.g in a foreach or for loop ins$$anonymous$$d of searching in the whole scene in each frame first, what's usually not very performant.

avatar image Vinnihu · Mar 20, 2020 at 08:20 PM 0
Share

nextAttack gets reset every time on line 24

 nextAttack = attackInterval;


avatar image ShadyProductions Vinnihu · Mar 20, 2020 at 08:26 PM 0
Share

I just noticed, well then it should work fine. $$anonymous$$aybe you should add a debug line, and it shouldn't be called every frame.

With caching I mean putting this line enemyList = GameObject.FindGameObjectsWithTag("Enemy");

in Start() only.

avatar image BBIT-SOLUTIONS ShadyProductions · Mar 20, 2020 at 08:29 PM 0
Share

Yes, if no new enemies are spawned after the scene has started, during the game, then this is also an alternative :-)

avatar image BBIT-SOLUTIONS · Mar 20, 2020 at 08:22 PM 0
Share

To point 1.) Line 24 actually resets the nextAttack. You just should initialize it in your Start() method also. To point 2.) I agree

And then you should maybe check in line 22 for 0f ins$$anonymous$$d of 0, because it is a float and that causes sometimes problems. The algortithm itself seems to be ok in my oppinion.

avatar image Vinnihu BBIT-SOLUTIONS · Mar 20, 2020 at 08:44 PM 0
Share

Still doesn't work. I really don't get it...

avatar image BBIT-SOLUTIONS Vinnihu · Mar 20, 2020 at 08:55 PM 0
Share

Could you maybe show your latest script again?

Show more comments
avatar image Vinnihu · Mar 21, 2020 at 11:01 AM 1
Share

I just saw that the bullet prefab had the tower script on it.. Took me like 2h to solve this problem.. Thank you guys anyways :)

avatar image BBIT-SOLUTIONS Vinnihu · Mar 21, 2020 at 02:21 PM 0
Share

Oh ok, that explains it of course :-D

Then you have called it each time on Start() and Spawned the next bullet immediatelly.

Thanks for your update, good to know, that at least the Script works.

Follow this Question

Answers Answers and Comments

159 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

Related Questions

Timer Progress Bar 2 Answers

how to Subtract string from TimeSpan 0 Answers

UnityOSC and timer 0 Answers

Resetting Timer Using PlayerPrefs? (JS) 1 Answer

Timer with Coroutine 3 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