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 DayyanSisson · Aug 19, 2011 at 10:01 PM · collisionif-statementsexplosionfindgameobjectswithtagcollisiondetection

Different Explosion for Different Collisions

Before I ask, I'll tell you one thing, I already looked at the other questions about this subject. They didn't give me an answer, and I figured this question is specific to me, so please don't refer me to another question unless the question got answered. So far, all the other questions like this haven't been answered (as far as I know), that's why I'm asking.

Okay here's the question: I'm making a space sim, and so when I hit different targets, a different explosion occurs. I wrote a script, but I don't have any clue about how to use raycasting (I don't even know what raycasting is) so I use collisions. I'm still not good with collisions, so there are problems in the script. Amazingly, when I finished writing it, there were no errors (except for one, I forgot to put a parenthesis). So there are no errors, and the variables work, but the script doesn't work. Here's the script:

 var explosionCapital : GameObject;
 var explosionFrigate : GameObject;
 var explosionFighter : GameObject;
 var explosionShields : GameObject;
 
 function OnCollisionEnter (collision : Collision) {
     
     if(Collision.FindGameObjectsWithTag("Droid Capital"))
 Instantiate(explosionCapital.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Human Capital"))
 Instantiate(explosionCapital.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Droid Frigate"))
 Instantiate(explosionFrigate.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Human Frigate"))
 Instantiate(explosionFrigate.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Droid Fighter"))
 Instantiate(explosionFighter.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Human Fighter"))
 Instantiate(explosionFighter.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Droid Shields"))
 Instantiate(explosionShields.transform, transform.position, transform.rotation);
 
     if(Collision.FindGameObjectsWithTag("Human Shields"))
 Instantiate(explosionShields.transform, transform.position, transform.rotation);
 
 Destroy(gameObject);
 
 }

As you can see it's just the same lines of code over and over again, just with different variables. It's pretty simple, but it doesn't work. Your help would be greatly appreciated. Thanks in advance.

Comment
Add comment · Show 2
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 Bovine · Aug 19, 2011 at 10:30 PM 0
Share

You need to define in what way it doesn't work - have you established whether your OnCollisionEnter() method is being called? One simple way would be to print() something, say the gameobject's name at the top of your method and see if it appears in your console.

Looking at your code again you probably want to be comparing the tag of the gameobject involved in the collision and not calling FindGameObjectsWithTag().

http://unity3d.com/support/documentation/ScriptReference/Component.CompareTag.html

avatar image DayyanSisson · Aug 19, 2011 at 11:44 PM 0
Share

Well that's the problem I'm not sure why it's not working. I know for sure though, that it's colliding, because the laser still do damage to whatever object I'm hitting. It's just no instantiating an explosion. Also it doesn't destroy the gameObject.

3 Replies

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

Answer by stopsecret · Aug 20, 2011 at 01:58 AM

What you do when finding the tag of the gameObject you hit is you make a statement similar to this:

if (collision.transform.tag == "Example"){

I recommend using '{' and '}' to start and end your 'if' statements, as this can help to keep things clearer. So, first of all, I think you have defined 'Collision' as 'collision', because of this, in the main script, you may want to call collision instead of Collision. Just another quick point: when defining your explosionCapital objects etc. you may want to say 'var explosionCapital : Transform;', it's basically the same thing as 'var explosionCapital : GameObject;', except in your script you don't need to say explosionCapital.transform- Also, one last tip about your scripting in general: Unity Scripting reference, whenever you have a question, before asking it on UnityAnswers (Not that we don't like answering your questions ;) you may want to search here: http://unity3d.com/support/documentation/ScriptReference/index.html the scripting documentation is how I got to learned so much! Hope this was of help to you!

Comment
Add comment · Show 8 · 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 Eric5h5 · Aug 20, 2011 at 02:03 AM 1
Share

It's better/faster to use CompareTag rather than "tag ==".

avatar image stopsecret · Aug 20, 2011 at 02:07 AM 0
Share

Really? $$anonymous$$y bad, thanks @Eric5h5 :) sorry @nighthawx349

avatar image DayyanSisson · Aug 20, 2011 at 02:09 AM 0
Share

Well, making it a transform mean that it would have to be in the Hierarchy for it to be used?

avatar image stopsecret · Aug 20, 2011 at 02:10 AM 0
Share

Actually, no, you can drag prefabs into the transform slot-

avatar image Bovine · Aug 20, 2011 at 02:57 AM 0
Share

I would suggest the opposite for braces on if statements - the opening brace and closing brace should be the same level. I have never worked anywhere in the software industry - where I work now - where it has been acceptable to open a brace at the end of an if statement or other scope beginning statement.

Always use braces, always indent the code within the braces an additional level.

This may spark a religious war, but in my experience, it's never been accceptable anywhere commercially to do as suggested by stopsecret.

Putting your curly brackets at the end of a statement loses the start of that 'scope' and makes it harder to read, especially when you have several braces involved.

Show more comments
avatar image
1

Answer by Eric5h5 · Aug 19, 2011 at 10:05 PM

FindGameObjectsWithTag returns an array of GameObjects, so it isn't what you use here at all. You should be using CompareTag. Also, you're using the type of the variable instead of the variable name; it should be the other way around.

Comment
Add comment · Show 3 · 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 DayyanSisson · Aug 19, 2011 at 10:11 PM 0
Share

Sorry, I'm pretty new to scripting. What do you mean by variable type ins$$anonymous$$d of variable name. Sorry, wrong question. How would that translate into script?

avatar image Bovine · Aug 19, 2011 at 10:32 PM 0
Share

Variable type is a very fundamental scripting concept - you should probably take some time to read up on c#, JavaScript or boo - whicherver language you prefer. People helping you need to be able to talk at a particular technical level and it becomes unworkable if they have to explain even the basics.

avatar image DayyanSisson · Aug 19, 2011 at 10:38 PM 0
Share

Yeah, I know what a variable type is, that's why I said "wrong question." I guess I didn't make that clear. Sorry about that. What I meant was, how would I write it the other way around. That, I don't know.

avatar image
0

Answer by stopsecret · Aug 20, 2011 at 01:49 PM

Ok, so I used the good ol' drag-the-script-into-Unity-and-debug-it, so here are the following problems: 1. when saying collision.CompareTag, you are calling CompareTag on a Collision, which will give you an error, you should instead use collision.transform.CompareTag - 2. when calling Instantiate(), you can just type something like explosionCapital in, you don't need transform.explosionCapital. Also, bizarre as it is, I had to add a rigid body in order for it to detect collisions at all. Somebody on here can probably come up with a better method than that... here's my example of a debugged part of your script:

if(collision.gameObject.CompareTag("Droid Capital")) { Instantiate(explosionCapital, transform.position, transform.rotation); }

hopefully this will help.

@Bovine, i'm an independent guy, so being isolated from a lot of the Unity scripting community has caused some strange stuff, such as the { at the end of my if function etc. (note the 'fixed' brackets above :) and calling transform.tag == instead of CompareTag()... Sorry about that...

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 Bovine · Aug 22, 2011 at 05:36 PM 0
Share

Tbh some people do prefer the { at the end of a statement starting new scope - I think program$$anonymous$$g books have encouraged this practise as often it is notated in this way in the name of compressing the code onto fewer lines... it is ultimately personal preference :-)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Tank Missle Colide then explode and destroy 1 Answer

Collision detection general approaches in Unity3D for C# 1 Answer

How to detect an object inside another object? 1 Answer

Changing localScale on Collision2D 2 Answers

How to make OnParticleCollision affect multiple GameObject 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