# Different Explosion for Different Collisions

 0 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. more ▼ asked Aug 19 '11 at 10:01 PM Overlord 484 ● 68 ● 79 ● 90 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 Aug 19 '11 at 10:30 PM Bovine 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. Aug 19 '11 at 11:44 PM Overlord add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

3 answers: sort voted first
 0 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. more ▼ answered Aug 19 '11 at 10:05 PM Eric5h5 81.5k ● 42 ● 133 ● 529 Sorry, I'm pretty new to scripting. What do you mean by variable type instead of variable name. Sorry, wrong question. How would that translate into script? Aug 19 '11 at 10:11 PM Overlord 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. Aug 19 '11 at 10:32 PM Bovine 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. Aug 19 '11 at 10:38 PM Overlord add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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... more ▼ answered Aug 20 '11 at 01:49 PM stopsecret 56 ● 5 ● 7 ● 10 Tbh some people do prefer the { at the end of a statement starting new scope - I think programming 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 :-) Aug 22 '11 at 05:36 PM Bovine add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x2584
x163
x84
x66
x57

asked: Aug 19 '11 at 10:01 PM

Seen: 1568 times

Last Updated: Aug 22 '11 at 05:36 PM