Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mmssyy25 · Jun 18, 2018 at 07:16 PM · triggerspawningitems

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".

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

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 mmssyy25 · Jun 18, 2018 at 09:31 PM 0
Share

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. alt text

alt text

capture2.png (135.8 kB)
avatar image
0

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 :)

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 mmssyy25 · Jun 19, 2018 at 06:33 AM 0
Share

could i get this in c#?

avatar image Harinezumi mmssyy25 · Jun 19, 2018 at 07:34 AM 0
Share

This is C#.
However, I wouldn't recommend this approach. See my answer below for something more robust.

avatar image Raimi Harinezumi · Jun 19, 2018 at 01:29 PM 1
Share

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.

avatar image Raimi mmssyy25 · Jun 19, 2018 at 01:31 PM 0
Share

like @Harinezumi said, it IS C#. C# is the only language id recommend for Unity if you are serious about it.

avatar image
0

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 
 }
Comment
Add comment · 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

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

107 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

Related Questions

How to Trigger Animation After x Items Collected? 2 Answers

Activate an input to pickup or search objectes 1 Answer

Trigger or Colission not work if inside volume 2 Answers

Can't click gameobject when over another trigger? 1 Answer

Activate trigger if items colected 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