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.
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
– BovineWell 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.
– DayyanSisson