Hello, so I am creating a 2d shooting platformer, and for now you can shoot 2 different projectiles, and each of them generates a slightly different explosion.
What I am trying to do is: checking whats the GameObject the script is attached to when called ( because each projectile is a different prefab so it goes like this: pressing button1 generates Projectile1 generating an explosion1, pressing button2 generates Projectile2 generating an Explosion2).
Pressing X button and generating the projectile part is doing fine, however I cant find out how check in my script responsible for the explosions, how to check if the gameobject is the type of projectile 1 or 2 , so it instantiates the desired explosion.
I know I could make individual scripts for each projectile prefab and attach them togheter, but I think that would be very inefficient. I was searching and found the transform.Find function for this (as shown in the script) but it doesnt seem to work the way I intend to.
My explosion script:
public GameObject explosion;
private int timesTouched;
GameObject objName;
// Use this for initialization
void Start () {
objName = transform.Find("").gameObject;
timesTouched = 0;
}
void OnCollisionEnter2D(Collision2D collision) {
if (objName.name = missil) {
Explode();
}
// variables are not declared yet, just made the statement below so its clear what i am trying to achieve
if (objName.name = missilMid) {
ExplodeMid();
}
}
void Explode() {
Instantiate(explosion, transform.position, Quaternion.identity);
timesTouched++;
if (timesTouched >= 1) {
Destroy(gameObject);
}
}
}