2D Damage Problems.

Hello guys,

It’s me again still working on that Brackeys tutorial, but after sorting through the shooting problems and the respawning not working at all until I reworked the code, I am now working on getting damage to work between objects and having problems getting it to register.

The code with the collision between the bullet and AlienShip at the moment registers and shows damage only for one hit, so, I am hoping that one of you knowledgeable guys might be able to point me in the right direction :).

In any case any assistance will be greatly appreciated!

The code is as follows, just forget about how inefficient the code is :eyes::

using UnityEngine;
using System.Collections;

public class BulletCollision : MonoBehaviour {

    //link to the explosion prefab: Explosion_prefab
    public Transform xplosionPrefab;

    //link to EnemyMaster Script
    EnemyMaster enemyShip = new EnemyMaster() ;
    public int damageAmount = 20;



    //Hell, I hate the way MonoDevelop indents the code

    void OnCollisionEnter2D (Collision2D col) {
       
        print ("BulletCollision.cs");

       
        if (col.gameObject.tag == "Sphere") {
            print ("The bullet hit the SPHERE.......");
        }
       
        if (col.gameObject.tag == "Platform") {
            //make xplosion work on Platform objects for now
            //until I find a way to make the bullet pass thru the
            //platforms without needing to make an Xplosion occur
            //AND put it on all needed object without copying code
            //probably just use: if (  (col.obj.tag == blah) OR (col.obj.tab==blah)   )  { }
            //use explosion code between braces
            ContactPoint2D contact = col.contacts [0];
            Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
            Vector3 pos = contact.point;
           
            Instantiate (xplosionPrefab, pos,rot);

            print ("The bullet hit the PLATFORM........");
           
            //destroy the gameObject because the bullet/missile has run it's course
            Destroy (gameObject);

        }
       
        if (col.gameObject.tag == "AlienShip") {
            //make explosion occur on AlienShip
            ContactPoint2D contact = col.contacts [0];
            Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
            Vector3 pos = contact.point;
           
            Instantiate (xplosionPrefab, pos,rot);

            //destroy this gameobject
            //"this" being the prefab it is attached to - MissileGroup.
            Destroy (gameObject);

            enemyShip.EnemyDamage(damageAmount);
            print ("bullet hit the ALIENSHIP.  Health: "+enemyShip.GetHealth() );

        }
        //end those if's :)

       
    }//end OnCollisionEnter(col col)*/


}//end BulletCollision class()

hi again :smile:

try to switch lines 56 and 59.

hahaha :smile:.

I did try that just in case and that didn’t work so I did some investigating and I found that the error was at line 10 were I was using the new keyword. It was creating a “new” AlienShip instance everytime and then trying to take away the damageAmount from that so it was only ever going to work just the once!!!

What I needed to do was use SendMessage and call the appropriate function from the EnemyMaster class. Ala:

col.gameObject.SendMessage(“EnemyDamage”,20);

Don’t really understand how it finds the function - I suppose that’s part of Unity’s magic :sunglasses:.

Now I have to figure out how to Destroy the AlienShip, and use a Prefab for the explosion. That be fun :eyes:.

if you will destroy something from bulletCollision script, then: Destroy (col.gameobject);
you can place explosion from this script too:

public GameObject explosionPrefab;
//then in OnCollisionEnter2D
Instantiate (explosionPrefab, col.gameobject.transform.position, col.gameobject.transform.rotation);

or selfdestroy from alienship script. Something like:

public GameObject explosionPrefab;
void Update() {
if (health < 0) {
Instantiate ( explosionPrefab, transform.position, transform.rotation);
Destroy (gameobject);
}}

Sorry for the late reply … for the last couple of days I have been “extremely” busy with the martial arts and then managed to do my shoulder in doing the black belt class - ouch! Typing this message with the left-hand.

Anyway, thanks so much so giving me two ways to destroy the alienship, that has saved me about 2 weeks worth of hair-pulling and brain-wracking :smile:.

Regards,
CK.