- Home /
How to only destroy the object the i am in.
I am trying to make a item drop system. I want to have the drop to drop(spawn) in a random location(which works fine by using a gameobject as the spawn point ) and be destroyed so i can drop an item. the problem is i don't know how to only destroy the item that i am standing in && when i press "e".
Answer by tormentoarmagedoom · Jun 18, 2018 at 07:31 PM
Good day.
I will say the tools/functions you need. But is up to you to go find how they work...
You need first to know what object is that. and assign it to a variable. Once you have the object asigned to a variable, need to detect if "Input keycode.E" is true. So you can then use Destroy (Object).
But how to know what object is? there are inifite ways to do it... what youwant to destroy? the spawnpoint? the object droped? or what ?
When you Instantiate something, you can stoer it in a variable so you can continue changing things of the instantiated object once is in the scene.
GameObject ObjectInstantiated = Instatiate(prefab, position, rotation);
Destoy (ObjectInstantiated);
IF you want to destroy the spawn point... you know what object is, just need to destroy it..
Or whats the question you have now i explained this? (Answer with explicit quesiton of what you have, and what oyu want. As a comment)
Bye!
the images show the drop. it falls from the sky and lands some where on the map. i need to be able to stand in the object(crate) press e and destroy only the object(crate) that i am standing in. there will be multiple of this game object so i only want to destroy the one i am standing in.
Answer by Raimi · Jun 19, 2018 at 03:28 AM
void Update()
{
if(Input.GetKeyUp(KeyCode.e))
{
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
foreach(Collider c in hitColliders)
{
if(c.tag == "ItemTag")
{
Destroy(c.gameObject);
}
}
}
}
I wrote this quickly and haven't had time to test it, but it should work. Hope it helps :)
This is C#.
However, I wouldn't recommend this approach. See my answer below for something more robust.
I was going to post basically the same thing using OnTrigger, but it seemed to convoluted for this simple use case, although it would work too.
OnTrigger can be unreliable. Sometimes it will fire when entering but not when exiting.
like @Harinezumi said, it IS C#. C# is the only language id recommend for Unity if you are serious about it.
Answer by Harinezumi · Jun 19, 2018 at 07:46 AM
Create a script, add it to the (prefab of) item drop, add a trigger collider to it, then in its OnTriggerEnter()
, if it is the player that entered it (you can check the tag of the Collider that entered the trigger), add it to a list of item drops in range in the player; in OnTriggerExit()
, do the reverse. When the player presses "E", one/the last, or all of the item drops in range can be destroyed.
Some code:
public class ItemDrop : MonoBehaviour {
private void OnTriggerEnter (Collider other) {
Player player = other.gameObject.GetComponent<Player>(); // assuming the player has a script with name Player
if (player != null) { // the object that entered the trigger is a player
player.AddItemDrop(this);
}
}
private void OnTriggerExit (Collider other) {
Player player = other.gameObject.GetComponent<Player>(); // assuming the player has a script with name Player
if (player != null) { // the object that entered the trigger is a player
player.RemoveItemDrop(this);
}
}
}
In the Player script you need the following:
private List<ItemDrop> itemDropsInRange = new List<ItemDrop>(); // you need using System.Collections.Generic; at the top of your source file for this to be included
public void AddItemDrop (ItemDrop itemDrop) {
if (!itemDropsInRange.Contains(itemDrop) { itemDropsInRange.Add(itemDrop); }
}
public void RemoveItemDrop (ItemDrop itemDrop) {
itemDropsInRange.Remove(itemDrop);
}
// when handling the "E" key
if (Input.GetKeyDown(KeyCode.E) {
if (itemDropsInRange.Count > 0) {
// change to this if all the items should get picked up: while (itemDropsInRange.Count > 0) {
PickUpItem(itemDropsInRange[itemDropsInRange.Count - 1]);
}
}
private void PickUpItem (ItemDrop itemDrop) {
RemoveItemDrop(itemDrop);
// apply the effect of picking up the item drop, like adding points, adding it to inventory, etc.
Destroy(itemDrop.gameObject); // this is optional, if the item drop gets destroyed when picked
}