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 Alter · Feb 10, 2013 at 08:43 PM · c#findgameobjectswithtag

Detect clone using FindGameObjectsWithTag method.

Hi guys, long story short I'm looking to find a way to count targets as they appear on the screen. If I have one already in the scene I have no issues, it counts. But since I'm using a spawn script it won't count the clones, any ideas? PS: I'm still new to scripting, this was created using the BurgZerg tutorials, Cheers guys!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerTargets : MonoBehaviour {
     
     
     public List<Transform> targets;
     private Transform selectedTarget;        
     private GameObject target;
 
     void Start () {
 
         targets = new List<Transform> ();
         AddAllEnemies ();
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
     
     void OnGUI() {
         GUI.Label (new Rect (10, 70, 1000, 20), "Enemies: " + targets.ToString ());    
     }
     
     public void AddAllEnemies ()
     {
         targets.Clear ();
         GameObject[] go = GameObject.FindGameObjectsWithTag ("enemy");
 
         foreach (GameObject enemy in go)
         AddTarget (enemy.transform);
     }
 
     public void AddTarget (Transform enemy)
     {
         targets.Add (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

· Add your reply
  • Sort: 
avatar image
1

Answer by cdrandin · Feb 10, 2013 at 08:54 PM

I don't know I tend to be picky on some thing, but why not dedicate a script solely for focusing on spawning? This is a lot more intuitive and encapsulates it from all other objects. If you insist on your method, then when you create the clone, create a tag "Clone". Then use GameObject.FindObjectsWithTag("Clone") which should find all your clone objects and put them into a list.

  • http://docs.unity3d.com/Documentation/Components/Tags.html

So, your clone should obviously be a prefab, just give it a "Clone" tag and it should all work nicely.

Comment
Add comment · Show 2 · 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 Alter · Feb 10, 2013 at 09:06 PM 0
Share

I went with your method of adding it to the 'spawn' itself and it works perfectly. Thanks- I don;t know why I didn't go that way in the first place.

avatar image cdrandin · Feb 10, 2013 at 09:49 PM 0
Share

It is just experience and now you know and you can expand that simple encapsulation idea to many other routines as well.

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

9 People are following this question.

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

Finding closest object to player 3 Answers

Help with GameObject.FindGameObjectsWithTag 1 Answer

Using a Parameterized arraylist (C#)??? 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