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 Voridian · Jul 22, 2014 at 07:58 AM · c#list

need help adding all gameobjects with a specific name to a list

hello again, can anybody help me add all gameobject with the name Hostile to a list. sombody else helped me make it but i cant get it working though

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PistolRay : MonoBehaviour {
 
     bool canFire = true;
     public GameObject decal;
     public float clipAmmo = 8;
     public float storedAmmo = 80;
     float missingShots = 0;
     GameObject player;
     GameObject characterControler;
     bool reloading = false;
     private List<EnemyAi> enemies = new List<EnemyAi>();
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         player = GameObject.Find("Player");
         CharacterControler characterControler = player.GetComponent<CharacterControler>();
         characterControler.ammoCount = clipAmmo;
         characterControler.totalAmmo = storedAmmo;
         enemies.Add(EnemyAi);
         //HERE^
         RaycastHit hit;
         Ray shooterRay = new Ray(transform.position, transform.forward);
 
         if(Input.GetButton("Fire1") && canFire && clipAmmo > 0){
             if(GameObject.Find ("EnemyAi") != null){
                 foreach(EnemyAi enemy in enemies){
                 Debug.Log("Test");
             EnemyAi enemyAi = enemy.GetComponent<EnemyAi>();
             enemyAi.couldHearGun = true;
             StartCoroutine (SoundCooldown(1));
             }
         }
         }
 
         if(Input.GetButton("Fire1") && canFire && clipAmmo > 0){
             Debug.DrawRay (transform.position, transform.forward, Color.green);
                 if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){
                 if(hit.collider.tag == "Enemy"){
                     hit.transform.GetComponent<EnemyCC>().health -= 15;
                 }
             }
             var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             Instantiate(decal, hit.point, hitRotation);    
             //GameObject decalInstance = 
             //decal.transform.parent = hit.transform;
             canFire = false;
             clipAmmo -= 1;
             missingShots += 1;
             StartCoroutine(FireRate(1));
             }
         if(Input.GetButtonDown("Fire1") && canFire && clipAmmo == 0 && !reloading){
             reloading = true;
             StartCoroutine(ReloadTime(3));
         }
 
         if(Input.GetButtonDown("Reload") && storedAmmo != 8 && !reloading){
             reloading = true;
             StartCoroutine(ReloadTime(3));
         }
         }
     public IEnumerator FireRate (float delay){
         yield return new WaitForSeconds(delay);
         canFire = true;
     }
     public IEnumerator SoundCooldown(float delay){
         GameObject enemy = GameObject.Find("EnemyAI");
         EnemyAi enemyAi = enemy.GetComponent<EnemyAi>();
         yield return new WaitForSeconds(delay);
         enemyAi.couldHearGun = false;
 
     }
     public IEnumerator ReloadTime (float delay){
         yield return new WaitForSeconds(delay);
         reloading = false;
         if(storedAmmo >= 8){
             storedAmmo -= missingShots;
             clipAmmo += missingShots;
             missingShots = 0;
         }
     }
 }
 



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
Best Answer

Answer by Tehnique · Jul 22, 2014 at 08:20 AM

I'm not sure what you are looking for, but maybe you want this at line 23:

 var enemyObjects =  GameObject.FindGameObjectsWithTag ("EnemyAi");
 foreach(GameObject enemyObj in enemyObjects)
 {
     EnemyAi enemyAi = enemyObj.GetComponent<EnemyAi>();
     if(enemyAi != null)
     {
       enemies.Add(EnemyAi);
     }
 }

For the code above to work, you need to tag all your EnemyAi objects with the tag "EnemyAi". It will look for them by tag, not by name, and this is the way to do it, finding by name should be avoided.

Comment
Add comment · Show 5 · 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 Voridian · Jul 22, 2014 at 08:31 AM 0
Share

Thanks for the reply, i put that code in and got an error: EnemyAI enemyAi = enemyObj.GetComponent();

'UnityEngine.Object' does not contain a definition for 'GetComponent' and no extension method 'GetComponent' accepting a first argument of type 'UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp)

avatar image T27M · Jul 22, 2014 at 08:35 AM 0
Share

Think it was a typo, it should be

 foreach(GameObject enemyObj in enemyObjects)
avatar image Tehnique · Jul 22, 2014 at 08:37 AM 0
Share

That's right, sorry, will edit my answer. Also, don't forget to tag your objects.

avatar image Voridian · Jul 22, 2014 at 09:20 AM 0
Share

thanks for the help it all works now. it causes a TON of lag any time i shoot though now

avatar image Tehnique · Jul 22, 2014 at 10:34 AM 0
Share

Yes, because GameObject.FindGameObjectsWithTag is resource intenssive. If you don't add enemies dinamically (during the game), you can move the code that fills your enemy list to the Start method. That will reduce lag.

If you do add them dinamically, try to add them to the list when you instantiate them, so you don't have to look for anything during Update.

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

23 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

Related Questions

A node in a childnode? 1 Answer

C# Adding Multiple Elements to a List on One Line 5 Answers

C# how to create a Descending GUI List 1 Answer

Printing an ordered list 1 Answer

Output a string list 1 Answer


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