Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 OtsegoDoom · May 19, 2011 at 01:13 AM · javascriptcollisiontriggerdestroytag

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.

Comment
Add comment · Show 4
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 jeffpk · May 19, 2011 at 01:37 AM 0
Share

"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.

avatar image Joshua · May 19, 2011 at 02:57 AM 0
Share

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.

avatar image PrimeDerektive · May 19, 2011 at 02:09 PM 1
Share

Jeff, that's fundamentally untrue. Since 3.x, there is practically no difference between pragma strict UnityScript and C#, besides obvious syntactical differences.

avatar image chemicalvamp · Oct 13, 2011 at 07:33 AM 0
Share

C# is 20x faster than javascript, something worth remembering in an engine restricted to a single thread.

6 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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 );
     }
 }
 
 }

Comment
Add comment · Show 4 · 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 OtsegoDoom · May 20, 2011 at 04:18 PM 0
Share

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.

avatar image OtsegoDoom · Jun 10, 2011 at 12:06 AM 0
Share

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!

avatar image Joshua · Jun 10, 2011 at 12:11 AM 1
Share

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

avatar image demize2010 · Jun 10, 2011 at 01:06 AM 1
Share

Unity has some amazing documentation most of the time :)

avatar image
2

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).

Comment
Add comment · Show 2 · 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 Jason_DB · May 19, 2011 at 01:57 AM 0
Share

I take it that the site no longer notifies you when an answer is posted for the question you are viewing...

avatar image Joshua · May 19, 2011 at 02:59 AM 0
Share

Nope, already posted about it on the bugs/complaints 'question' :p. Annoying, isn't it? Sympathy upvote! :p

avatar image
2

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 :)

Comment
Add comment · Show 2 · 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 Bunny83 · May 19, 2011 at 02:48 AM 0
Share

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

avatar image Joshua · May 19, 2011 at 03:01 AM 0
Share

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.

avatar image
1

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

Comment
Add comment · Show 4 · 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 Bunny83 · May 19, 2011 at 01:39 AM 0
Share

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.

avatar image Joshua · May 19, 2011 at 01:41 AM 0
Share

Heh yeah I also only know about it because I was browsing the docs :p

avatar image Dreamora · May 21, 2011 at 12:35 AM 0
Share

I heard about it through the changelogs. its the fastest and easiest way to get your head around new stuff normally :)

Show more comments
avatar image
0

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

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 Joshua · May 19, 2011 at 01:37 AM 0
Share

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

  • 1
  • 2
  • ›

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Change scene with trigger collision not working. 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