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 BANNERTM · Jun 09, 2012 at 06:07 PM · low fps

GameObject.FindGameObjectsWithTag-low FPS

I use GameObject.FindGameobjectWithTag for finding enemies , but when i have many turrels with this script, fps is very low, i use this function in update(), but i heard that it needs to be in Awake(), but when I fill code in awake() turrels dont work I actually use Code FindClosestEnemy and write in c#. Thanks a lot.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class cannon : MonoBehaviour {
     
  Transform target; 
   
 public Transform bullet;
 public float attackRange = 30.0f;
 public float shootAngleDistance = 10.0f;
 public float gundamag = 30;    
 
     
 
     
 float _time = 5.0f;
 double t = 0.3f;
     
 float objectdefault_y_position = 21.55736f;    
  
 void Awake(){
 
     
         
 }
 void Start(){
     
     }
 
 
     
 void Update(){
     
 
         
     GameObject closest=null;
         float distance = 5000f;    
         
         
         
         foreach(var enemy in Enemy_test_Scrpt.AllEnemies)
   {
         
         var diff=(enemy.transform.position-transform.position);    
         var curDistance=diff.sqrMagnitude;    
         if(curDistance<distance)
             {
                 closest=enemy;
                 distance=curDistance;
                 
             }
                     
   }    
         
     target=closest.transform;            
         
     
         







     
     
     
    var targetPoint = target.position;
    var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 4.0f);
 
    var forward = transform.TransformDirection(Vector3.forward); 
    var targetDir = target.position - transform.position; 
   
         
 RaycastHit hit;
         

         if(Physics.Raycast(transform.position,transform.forward,out hit,200f)){//стрельба
             Debug.DrawLine( transform.position, target.position, Color.red);
             
             
 
             Pause ();
                 
                 
             if(t>=0.5){
             
             if(hit.transform.gameObject.layer!=4){    
                 
                 hit.transform.gameObject.GetComponent<Enemy_test_Scrpt>().target_health-=gundamag;
                     }            
             t=0.3;    
             }
         }
     }
         
 void Pause(){
         
 if(t<0.5){
 t+=Time.deltaTime*(1.0/_time);    
 }    
     }
 }
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 BANNERTM · Jun 09, 2012 at 07:03 PM 0
Share

Exlain me please how to make array with enemies!

avatar image Bunny83 · Jun 10, 2012 at 10:30 AM 0
Share

Next time please format your code in a way it can be read. You had around 4-6 empty lines between each line of code and nothing was indented in a reasonable way.

avatar image BANNERTM · Jun 10, 2012 at 10:51 AM 0
Share

Ok, sorry. When I have 5-6 turrels at scene it works grat, but when ~30-50 it's so laggy

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jun 09, 2012 at 06:14 PM

Yep don't go calling FindGameObjectWithTag in Update(). Add the code to Start() and cache the results in a list or an array.

Or add a script to your enemies that stores them in a list that is a static:

   using System.Collections.Generic;

   public class EnemyList : MonoBehaviour 
   {
        public static List<GameObject> AllEnemies = new List<GameObject>();
        void Awake()
        {
             AllEnemies.Add(gameObject);
        }
        void OnDestroy()
        {
             AllEnemies.Remove(gameObject);
        }
   }

Now you can work through your enemies using:

   foreach(var enemy in EnemyList.AllEnemies)
   {
   }
Comment
Add comment · Show 8 · 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 BANNERTM · Jun 09, 2012 at 06:18 PM 0
Share

But enemies apears during game.

avatar image whydoidoit · Jun 09, 2012 at 06:28 PM 0
Share

The code I added to the answer enables enemies to be created during the game - you should use that rather than caching in Start().

avatar image BANNERTM · Jun 09, 2012 at 06:41 PM 0
Share

Sorry, but I dont understand what I should do in foreach? Thanks.

avatar image whydoidoit · Jun 09, 2012 at 07:34 PM 0
Share

Well you add that script to your enemies - now you have a List of game objects which is like an array. You can do with it whatever you did with your FindObjectsWithTag - but no need to find them.

You can do EnemyList.AllEnemies[0] etc. It's length is in EnemyList.AllEnemies.Count and foreach lets you run through the whole list.

avatar image BANNERTM · Jun 09, 2012 at 11:14 PM 0
Share

Thanks it works, but where i should fill foreach? Into Update or Start?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Low FPS after loading a scene 1 Answer

Scroll rect on mobile? 0 Answers

low fps when looking in a direction with many objects 2 Answers

Blank scene, 2 sprites, 32 FPS 2 Answers

Serious performance issues 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