Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Letizeus · Jul 10, 2020 at 08:06 PM · destroymultiple objects

Destroy one object from multiple others with the same tag

Code:

public class Pickups_Manager : MonoBehaviour { public Transform player; bool maxDist = false; Player_Movement_Manager playerMove;

 public Text Info_Text;

 public Keyboard Keycodes;

 public GameObject[] Healths;

 float dist;

 [System.Serializable]
 public class Keyboard
 {
     public KeyCode Pickup = KeyCode.E;
 }

 void Start()
 {
     Info_Text = GameObject.Find("Info_Text").GetComponent<Text>();
     playerMove = GameObject.Find("Player").GetComponent<Player_Movement_Manager>();
     Healths = GameObject.FindGameObjectsWithTag("Health");
 }

 void Update()
 {
     if (Healths != null)
     {
         foreach (GameObject Health in Healths)
         {
             if (Health != null)
             {
                 dist = Vector3.Distance(Health.gameObject.transform.position, player.position);
                 if (this.GetComponent<Player_Movement_Manager>().hasEyeOnHealth && maxDist && Input.GetKey(Keycodes.Pickup))
                 {
                     if (GetComponent<Player_Health_Manager>().Current_Health < GetComponent<Player_Health_Manager>().Maximum_Health)
                     {
                         Destroy(Health.gameObject);
                     }
                 }
                 if (dist <= 5)
                 {
                     maxDist = true;
                     break;
                 }
                 else
                 {
                     maxDist = false;
                 }
             }
         }
         if (this.GetComponent<Player_Movement_Manager>().hasEyeOnHealth && maxDist)
         {
             Info_Text.text = "Press " + Keycodes.Pickup + " To Pick Up";

             if (Input.GetKey(Keycodes.Pickup))
             {
                 if (GetComponent<Player_Health_Manager>().Current_Health < GetComponent<Player_Health_Manager>().Maximum_Health)
                 {
                     //Calls a method that heals the player
                     //The method can be found in player_Health_Manager
                     this.transform.SendMessage("Pickup_Heart");
                     Info_Text.text = "";
                 }
             }
         }
         if (!playerMove.hasEyeOnHealth)
         {
             Info_Text.text = "";
         }
     }
 }

}

So what i am trying to accomplish here is that when the player takes on of the Health_Pickups he will get healed and the one he took will get destroyed. The problem here is that when I try to take one, then all of the others will also get destroyed with it and I do not want that to happen...

If you are wondering what hasEyeOnHealth is, I just have a raycast that goes from the player to check if I am looking at the Health_Pickup with the tag "Health" or not

And also please I do not want a solution with onTriggerEnter.

Thanks for everyone who takes some of his time to help me!

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 UnityedWeStand · Jul 10, 2020 at 09:09 PM

The problem is that if (this.GetComponent().hasEyeOnHealth && maxDist && Input.GetKey(Keycodes.Pickup)) and if (GetComponent().Current_Health < GetComponent().Maximum_Health) will evaluate to true for every health pack as long as you are in range and looking at one health pack. This means on the parent foreach loop, you will be destroying every single health pack.

The easiest solution would be to store whatever health pack the raycast generated by hasEyeOnHealth is hitting. Since you already are checking whether the GameObject hit by the raycast has the correct tag, it would be only a few more lines of code to store a reference to that particular health pack and destroy only that one when the player presses the pickup key.

Comment
Add comment · Show 1 · 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 Letizeus · Jul 11, 2020 at 12:16 AM 0
Share

Thank you so much for your answer! How did I miss that! I removed the Foreach loop and added the this line in the raycast: Health = HitInfo.collider.gameObject; This way it will always store the gameobject i am looking at and it worked just fine. Thank you!

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

137 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

Related Questions

How can i destroy multiple objects with tag? 2 Answers

multiple objects with same script, how to destroy one of them ? 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

multiple objects with same script, how to destroy one of them ? 1 Answer

How can I Destroy and respawn random Objects from object 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