- Home /
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!
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.
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
![](https://koobas.hobune.stream/wayback/20220613002922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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