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
0
Question by Vecca · Dec 10, 2012 at 09:31 PM · destroy object

How to destroy obj B when pickup obj A?

I'm a bit of a newbie to Unity, I'm not a programmer at all, so all I know is some basic scripting stuff.

I'm trying to destroy a rock (object B) when I pick up a gem (object A). No need for counters or anything.

So far I have this for picking up the 'gem', or object A, which works perfectly:

 var CollectSound : AudioClip;
 
 function OnTriggerEnter (other : Collider) {
 
     other.audio.PlayOneShot(CollectSound);
         gameObject.active = true;
     DestroyObject(gameObject);
 
 }

This script I added on my gem/obj A, it's tagged as "gem". The rock is tagged as "Rock".

I've been searching the documentation, the web and Unity forums for hours now trying to figure out how to do this, but I've not got a clue. Please 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Dec 10, 2012 at 10:40 PM

You could make a public variable to link the rock to be destroyed when needed.

 var CollectSound : AudioClip;
 var remoteObject : GameObject; // NEW - drag the rock to this field on inspector
 
 function OnTriggerEnter (other : Collider) {
     other.audio.PlayOneShot(CollectSound);
     gameObject.active = true;
     Destroy(gameObject);
     Destroy(remoteObject);  // NEW
 }
 
 // Or...
 
 var CollectSound : AudioClip;
 
 function OnTriggerEnter (other : Collider) {
     other.audio.PlayOneShot(CollectSound);
     gameObject.active = true;
     Destroy(gameObject);
     Destroy(GameObject.FindWithTag("Rock"));  // NEW
 }

If anything confuses you I suggest that you read the scripting reference:

  • Scripting Reference

  • Scripting Reference: Accessing Other Game Objects

  • GameObject.FindWithTag

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
avatar image
0

Answer by HunterKrech · Dec 10, 2012 at 11:05 PM

add this to Gem A

var CollectSound : AudioClip;

var RockOne: GameObject; //Drag the rock object to this spot under the script component in unity

function OnCollision (other : Collider) {

if(collision.gameObject == RockOne){

Destroy(GameObject);

Destroy(RockOne);

}

}

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 Vecca · Dec 11, 2012 at 09:58 AM 0
Share

Thank you both, they both work wonderfully so I'll just pick one and go from there!

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

10 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

Related Questions

Destroy Objects to Load New Scene 3 Answers

Why does Destroy(this.gameObject) destroys a prefab that got nothing to do with the script 0 Answers

Change Score / On Click Destroy Enemy 1 Answer

Problem deleting object 1 Answer

detect Gameobject which is created from c# 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