- Home /
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.
"I'm just starting to take the plunge into javascript program$$anonymous$$g in Unity."
Piece of advice. There are sharks in those waters and they will bite your leg off. If your goal is to learn to write games, then drop Javascript and start learning c# now.
Javascript lacks the fundamental structures for doing well organized and structured code. Every Unity game project I know of that has started in Javascript (and I know of a number of them) has eventually had to drop it and move all their code over to C#.
Save yourself that pain and start there from the beginning.
Jeffpk, there are endless debates on JS vs C#, and this doesn't seem like an appropriate place to start an other one. He has started learning JS, which is imho a better choice for a beginner due to the slightly easier syntax and wider availability of examples. Also, learning C# if you know JS is not that big a change.
Jeff, that's fundamentally untrue. Since 3.x, there is practically no difference between pragma strict UnityScript and C#, besides obvious syntactical differences.
C# is 20x faster than javascript, something worth remembering in an engine restricted to a single thread.
Answer by Joshua · May 19, 2011 at 01:35 AM
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 );
}
}
}
Hi,
I've tried this script at home and at work in different project files. Both are Unity 3.3 I get the following error:
BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '()' was found.
and it says the error is at (15,31) which would be the first line that says:
sameTagObject .Destroy();
This script looks like it does everything I need, but I'm not sure how to get around that error. Any advice?
Thanks.
Hi,
After working with your script more I was able to get it working. I still keep getting errors with the .Destroy() component but I'm modified my application so it does something different with the objects that it identifies.
As you mentioned it's not the most efficient when it has a large number of objects on screen but it works for now.
Thanks again!
I'm sorry about the Destroy(), I was mistaken about how the function works. Replace someObject.Destroy() with Destroy( someObject ) I'll also edit my answer. One huge tip though, when ever you're not a hundred percent sure how a function works, just look it up in the docs. http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
Unity has some amazing documentation most of the time :)
Answer by Jason_DB · May 19, 2011 at 01:54 AM
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[i].transform.position) < triggerRadius);
Destroy(tempArray[i]);
}
(Hopefully this works, I am not somewhere i can test this right now).
I take it that the site no longer notifies you when an answer is posted for the question you are viewing...
Nope, already posted about it on the bugs/complaints 'question' :p. Annoying, isn't it? Sympathy upvote! :p
Answer by demize2010 · May 19, 2011 at 01:58 AM
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 ;)
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 ;)
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 :)
That's always my approach, but you should use Physics.OverlapSphere. It's does exactly what you wanted to do ;) http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html
Yeah, if you use overlapsphere and then iterate through them (like I did in my answer, in a less practical order - but check it for an example :p) you'd be using the most efficient method.
Answer by Dreamora · May 19, 2011 at 01:29 AM
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
Fine, OnDestroy comes a bit late :D. I see a big problem rising... A lot projects still use older versions of unity. Even our current project is still a 3.0 project. The documentation has been updated, but no hint that this is a new feature or that it doesn't exists in other versions of Unity.
Heh yeah I also only know about it because I was browsing the docs :p
I heard about it through the changelogs. its the fastest and easiest way to get your head around new stuff normally :)
Answer by kennypu · May 19, 2011 at 01:29 AM
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
What he means with "within the first obj. trigger radius" is all objects that are returned by http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerStay.html
Your answer
Follow this Question
Related Questions
How to make two objects collide using "Is Trigger" 1 Answer
Trigger not detecting tag 1 Answer
Why won't the collision work? 3 Answers
OnTriggerEnter Collision 1 Answer