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 Kebabs · Sep 26, 2013 at 02:39 PM · destroytagsmultiple-objects

Destroy multiple game objects when player press a button

Hi I am new to C# and Unity. I have searched the web and I am finding it hard to implement a game mechanic. I am working on a mechanic which takes away health to perform a special move. This special move will destroy all enemies with a certain tag, however I am not sure how to do this.

I have came across FindGameObjectsWithTag but as I have said I am not sure how to implement this. I have read the Unity script reference and not sure how to take whats there and put it in my code. If it helps heres the code I have at the moment.

     if (Input.GetKeyDown (KeyCode.Keypad8) & shieldHealth > 50)
     {
         shieldHealth -=50;
         
         print ("I have enough health");
     }

Any help is greatly appreciated. Thanks in advance.

Comment
Add comment · Show 2
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 Kebabs · Sep 26, 2013 at 04:32 PM 0
Share

Thank you all for your advice. I am trying to use the advice given and I'm not having much luck :(. Like I have said I am new to using C# and Unity. O$$anonymous$$ so heres my code at the moment.

int [] myArray;

void Start () { myArray = new int [3];

     myArray [0] = GameObject.FindGameObjectsWithTag ("blueEnemy");

}

and I get this error.

error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to int'

I'm finding it really hard to understand this. Not just the error code but how to get this mechanic working

I'm trying my best to match the advice given by others below.

avatar image Kebabs · Sep 26, 2013 at 06:40 PM 0
Share

I have it working now so for anyone searching this on google heres what i did in C#...

At the top I declared an array.

GameObject [] redEnemies = new GameObject [1];

In the start function I made redEnemies something...

redEnemies = GameObject.FindGameObjectsWithTag ("Red Enemy");

and in the if statement above I killed all gameObjects tagged "Red Enemy" if the player had more than 50 health and pressed the correct button.

THAN$$anonymous$$S TO EVERYONE WHO HELPED!! :)

2 Replies

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

Answer by Cherno · Sep 26, 2013 at 02:57 PM

 var Enemies: GameObject[];//declares an array of GameObjects
 //to be filled later
 
 function Start()
 {
      Enemies = GameObject.FindGameObjectsWithTag("Enemy");
      //fill the Enemies array with actual enemy GameObjects
      //"Enemy" is the tag your enemies have
 }

 function Update()
 {
      if (Input.GetKeyDown (KeyCode.Keypad8) & shieldHealth > 50)
      {
           shieldHealth -=50;
           for(var EnemyCurrent in Enemies)
           //iterates (loops) through your array, EnemyCurrent is a temporary 
           //variable given to the current array entry each loop start
           {
                Destroy (EnemyCurrent);//Destroys the current enemy object
           }
           print ("I have enough health");
       }
  }
         
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 Kebabs · Sep 26, 2013 at 03:08 PM 0
Share

Thanks dude! I use C# and will attempt this in my script and will comment on the outcome when its done. Thank you :)

avatar image Cherno · Sep 26, 2013 at 03:40 PM 0
Share

You're welcome. It shouldn't be too hard to convert it to C#, I'm sure you can manage. BTW, robertbu's answer above might be more elegant.

Note that if new enemies can be spawned after Start, you have to re-fill the array. In that case, it's probably best to just put the fill array code bit into the Update function just before the objects get destroyed (after "shieldHealth -=50;").

avatar image
0

Answer by Djspun · Sep 26, 2013 at 02:52 PM

this can help you destroy all object with same tag on key press you just need to set a partical affect to go off at the same time to look like an attack

http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy

Comment
Add comment · Show 4 · 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 Djspun · Sep 26, 2013 at 02:46 PM 0
Share

just giving a suggestion i no theirs a better ways to do it

avatar image Kebabs · Sep 26, 2013 at 03:07 PM 0
Share

Thank you for your help! This is great for single objects but not quite what I need. But thank you :)

avatar image Djspun · Sep 27, 2013 at 02:04 PM 0
Share

no problem im still looking for a way to kill off my enemy 1 buy 1 on 0 hp but its a hard one to figure out ill say have a good one

avatar image Kebabs · Sep 27, 2013 at 03:56 PM 0
Share

Could you use a timer on a Destroy like Destroy (gameObject, 5), or an if statement that killed your enemies only if their HP hit 0??

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Tagging Alternatives 2 Answers

How to destroy linked components when object is destroyed? 1 Answer

The AI-Players do not see new install (Instantiate) object (the ball) after they set the first goal in gates and old object (the ball) was destroyed. 0 Answers

Destroy only selected object 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