- Home /
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?
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:
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);
}
}
Thank you both, they both work wonderfully so I'll just pick one and go from there!
Your answer
![](https://koobas.hobune.stream/wayback/20220613085241im_/https://answers.unity.com/themes/thub/images/avi.jpg)