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
1
Question by jaeson · Dec 16, 2013 at 03:42 PM · gameobjectdestroypickup

PickUp (Destroy) gameObject one by one

Hi. I have 2 gameObject, let say the first one is phone and the second one is lighter. and also, i input a button 'T' to take. first, i want to take the phone, and then the lighter.. my issue here is both phone and lighter are gone when i press 'T' button. I want to take them one by one, not both of them! this is my code

 private var DrawGUI = false;
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.T))
     {
         itemCollected();
     }
 } 
 
 function OnTriggerEnter (info : Collider) 
 {
         if(info.tag == "Player")
         {
             DrawGUI = true;
         }
         
     }
     
 function OnGUI ()
 {
     if(DrawGUI == true)
     {
         GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
     }
 }
 
 function itemCollected()
 {    
     Destroy(gameObject);
 }

can't I use a script for it? or i must separate the script each objects? thx

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
1

Answer by Gruffy · Dec 19, 2013 at 04:00 PM

hey bud, here you go.

Start with this and we can move forward link text

Hope this helps you out dude.

gruffy..

Just to inform you, it is a full scene with two cube in there, both tagged seperately with phone and lighter, the logic comes form the CollectStuff script found attached to the character controller. Cheers bud:)


pickup (destroy) gameobject one by one.zip (23.6 kB)
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 jaeson · Dec 20, 2013 at 02:26 PM 0
Share

@Gruffy wow, it works like a charm!! really appreciate what u did.. :) i realize some of my mistake, since i saw your explanation on there.. thank u so much man :D

avatar image
0

Answer by Patrykgazing · Dec 16, 2013 at 04:01 PM

 private var DrawGUI = false;
 private var IsPlayerPickingItem = false;
  
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.T))
     {
         if(!IsPlayerPickingItem){
         IsPlayerPickingItem = true;
         itemCollected();
         IsPlayerPickingItem = false;
         }
     }
 }
  
 function OnTriggerEnter (info : Collider) 
 {
        if(info.tag == "Player")
        {
          DrawGUI = true;
        }
  
     }
  
 function OnGUI ()
 {
     if(DrawGUI == true)
     {
        GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
     }
 }
  
 function itemCollected()
 {   
     Destroy(gameObject);
 }
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 jaeson · Dec 18, 2013 at 04:09 PM 0
Share

it doesn't work.. :( but thanks for the solution :)

avatar image
0

Answer by emalb · Dec 16, 2013 at 04:04 PM

You could put a public variable in there that represents the number of presses required to collect your item, say numPresses. Then, in your Update function, each time you detect a key has been pressed you could decrease the numPresses variable and only call itemCollected when it reaches zero.

If you set the numPresses to 1 for your phone and 2 for your lighter (in the Inspector) you should be able to get this to do what you need.

Comment
Add comment · Show 6 · 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 jaeson · Dec 18, 2013 at 04:07 PM 0
Share

anyway i tried your solution with public variable, and do like what you told me above, i can take the phone that set to 1 but i cannot take the lighter that set to 2..

but since i am a beginner on javascript and unity, maybe i did wrong.. :/

can u write the script here? thanks anyway for the solution and sorry if i am inappropriate :/

avatar image emalb · Dec 19, 2013 at 10:58 AM 0
Share

I'm happy to help, but I would like to see your latest script for this. It saves me actually doing the work, but it also makes it easier for myself and others to see your thought processes and help you find your own way around it.

avatar image jaeson · Dec 19, 2013 at 01:45 PM 0
Share

i just modified some from this script.. but both phone & lighter still gone, and also there is no error.. :/

 #pragma strict
 
 var numPresses : int ;
 private var DrawGUI = false;
 
 
 function Update()
 {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) && numPresses == 1)
         GameObject.Destroy(gameObject.Find("Phone"));
     else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) && numPresses == 2)
         GameObject.DestroyObject(gameObject.Find("lighter"));
 } 
 
 function OnTriggerEnter (info : Collider) 
 {
         if(info.tag == "Player")
         {
             DrawGUI = true;
         }
         
     }
     
 function OnGUI ()
 {
     if(DrawGUI == true)
     {
         GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
     }
 }
 
avatar image Gruffy · Dec 19, 2013 at 02:29 PM 0
Share

I would put a trigger on each of your items to pick up Then i might use the tag dropdown in the inspcetor for each item and give them a meaningful name (like lighter, phone, etc)

On the player i would then use its own collider (whichever u have on it to check for trigger collisions)

The simplest implementation would be to conditionally check like so....

 function OnTriggerEnter(col: Collider)
 {
 if(col.gameObject.tag == "phone")
 {
  Destroy(this);
 //or col.gameObject.SetActive(false);
 }
 else if(col.gameObject.tag == "lighter")
 {
  Destroy(this);
 //or col.gameObject.SetActive(false);
 }
 else
 {
  return;
 
 }
 }
 
 }

lol, totally untested in Unity, but you hopefully get the idea.

Dont just copy it in there, have a gander at what its doing 1st my man.

Thanks for reading. Gruffy PS- comment back after youve had a figure out of it for a second, if its still not happening, I will write for you, im just a bit busy atm...anyhoo, let me know bud :)

avatar image jaeson · Dec 19, 2013 at 03:10 PM 0
Share

lol, thanks for that man.. i read n understanding the code, and also tried yours, but its still not happening.. :(

i did tag each objects for the trigger too, but still nothing happened ..

anyway FYI this one for FPS game, survival horror stuff :p

Show more comments

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

20 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

Related Questions

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

Collecting items when you destroy something? 1 Answer

Deleting GameObjects 1 Answer

Help destroying gameObject on RayCastHit? 1 Answer

The name "Destory" Does not exist in the current context 2 Answers


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