On Click, destroy object and any colliding object with the same tag.

Hi Everyone, I have to start off by saying I’m very new to programming. I’ve been a 3D artist for almost 14 years and I’m just starting to take the plunge into javascript programming in Unity. This question might seem pretty basic, but I’ve been struggling with it for weeks.

I have a screen full of different types of objects. If the user clicks on an object its health will drop. If its health drops below a certain point the object will be destroyed. I have that part working perfectly. This next part has me stumped:

When a specific object is destroyed, I want it to destroy all objects with the same name or tag (which ever is more efficient) that are within the first objects trigger radius.

I understand the logic for what I need to do, but I just don’t know enough about the syntax to link this all up. I’ve been struggling with this one for close to a month now. If anyone could point me in the right direction I’d really appreciate it.

Thanks.

Here, this script (untested!) will destroy all objects with the same name/tag within a certain range. It’s not a very efficient script if you have a lot of objects or if many have the same name/tag, because it will check the distance on all of them. In that case it would be more practically to use a trigger, but this script should be enough to point you in the right direction :).

var minDistance : float = 5.0;
function OnDestroy () {

var sameTagObjects = GameObject.FindGameObjectsWithTag(gameObject.tag);
var sameNameObjects = GameObject.Find(gameObject.name);

for (var sameTagObject : GameObject in sameTagObjects) {
    if(Vector3.Distance(transform.position,sameTagObject.transform.position) < minDistance) {
        Destroy( sameTagObject );
    }
}

for (var sameNameObject : GameObject in sameTagObjects) {
    if(Vector3.Distance(transform.position,sameNameObject .transform.position) < minDistance) {
        Destroy( sameNameObject );
    }
}

}

It sounds like there are two main parts that you need to write this (To destroy by tag). The first is to find all of the objects of a certain tag and put them into an array so that you can access them later. You can set the array in Start() in the script on your destructible object.

var tempArray : gameObject[];
tempArray = GameObjects.FindGameObjectsWithTag("tag");

Followed by a for loop to destroy everything in that array when the specific object is destroyed (put this in whatever function is called when it is destroyed).

for (var i = 0; i < tempArray.length; i++){
    if(Vector3.Distance(this.transform.position, tempArray*.transform.position) < triggerRadius);*

Destroy(tempArray*);*
}
(Hopefully this works, I am not somewhere i can test this right now).

Hey man,

First up if you’re using javascript make sure that you declare all your variables properly - as well as helping you plan stuff out, you’ll find a nice performance boost and that your code is compatible with Android :wink:

Using find object will certainly work, but be aware that it will search through each and every object in the scene - even if there is nothing near by! My preference would be to do a sphere raycast http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCastAll.html

This will build an array of colliers within your radius. You can then iterate through this array and compare the tags, you’ll only be doing the calculations you need to :wink:

If you’ve got this far it sounds like you have a good grasp of the basics so I’m going to skip writing the code as its quite late here but if you have troubles post em up :slight_smile:

Since Unity 3.3 or so there is the OnDestroy callback. You can use that to search the other objects by tag through FindGameObjectsByTag(…) and then destroy each of them in consequence for example.

destroying by name is basically impossible without specific hierarchies etc, as such I would consider it less efficient if doable at all

I can’t give you the specific code, since I don’t know what you mean by ‘within the first obj. trigger radius’, but to get all the objects with the same tag, you can use the function FindGameObjectsWithTag(String tag). more info

so this isnt hard to do, now i would use OnTriggerStay because its the easiest however i have noticed it can be some what processing expensive (especially if you have like thousands of them.
to reduce process load you can use the route of getting distance between objects and working from there like others have suggesed.
now in your kill function im sure you have it set up something like
if HP <=0 then
destroy game object
instead try putting that kill topic in the OnTriggerStay function

onTriggerStay
if HP<=0
-if Trigger.gameObject.tag==“whatever”
destroy(Trigger)(you could also set it so its a chain reaction this way by sending a kill msg instead which toggles this same behavior in its doomed counter parts)

then finally destroy gameobject

now this isnt quite right so you will need to look things up and play around a little but this will work

syntactiacally look up triggers, sendmessage, .tag .name all those identfiers