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.

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.

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 :wink: you may want to search here: Unity - Scripting API:
the scripting documentation is how I got to learned so much!
Hope this was of help to you!

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 :slight_smile: and calling transform.tag == instead of CompareTag()… Sorry about that…