Search code examples
unity-game-engineunityscript

Enemy Health reduction script


In my Unity3D game I have UFO enemies that fly around and spawn little minions and such. I wanted to make them harder to kill than the little minions so in their main script I put some health functions. I made this so when the player shoots at the UFO enough times, the UFO will be destroyed. (instead of shooting it once, then poof! It's gone.) It's a first person shooter and for some reason it doesn't work. Worse is that if it goes on long enough the game crashes and goes gray screen. I've looked on some script forums and haven't been able to find an answer yet. I might have misused a variable somewhere (one that works in C# and not in javascript) because I'm not too sure why it doesn't work.

    var UFOspeed : float = 0.2f; //the speed of it's flight

    var UFOmovement = true;
    var UFOmovement2 = false;
    var UFOhealth = 10;  //this is the amount of health I added. I'm unsure if in JS you have to put .0f at the end of numbers unless if it's a float. 


    function Start() 
    {
    UFOmove();
    }

    function Update () //this update just changes the direction of the movement of the UFO
    {
        if(UFOmovement == true && UFOmovement2 == false) 
        {
            this.transform.position.x += UFOspeed;
        }

        if(UFOmovement == true && UFOmovement2 == true) 
        {
             this.transform.position.z += UFOspeed; 
        }

        if(UFOmovement == false && UFOmovement2 == true) 
        {
            this.transform.position.x -= UFOspeed;
        }

        if(UFOmovement == false && UFOmovement2 == false) 
        {
            this.transform.position.z -= UFOspeed;
        }
    }



    function UFOmove () //UFO movement
    {
        for(i=1;i>0;i++) 
        {

    yield WaitForSeconds(1);

    UFOmovement2 = true;

    yield WaitForSeconds(1);

    UFOmovement = false;

    yield WaitForSeconds(1);

    UFOmovement2 = false;

    yield WaitForSeconds(1);

    UFOmovement = true;

    }
     }

      //This is where I have the bullet collision

       function OnCollisionEnter(collision : Collision) { 
        if (collision.gameObject.tag == "Bullet") //if the tagged object is Bullet
        {
            UFOhealth = UFOhealth - 1; //takes away from the health I put above
        }

        if (UFOhealth <=0)
        {
            Destroy(collision.gameObject, 3.0);
        }
   }

Thanks for your help!


Solution

  • I've never used unity3d, however looking at your code I can see a couple of major issues (in my opinion).

    In your OnCollisionEnter function, you're checking that the collision is with the bullet. If it is, then you're deducting 1 from the UFO health. So far so good.

    However then you are checking whether the health has got down to 0 - and, if yes, you're destroying the bullet. Instead, I would suggest that the bullet needs to be destroyed regardless of the UFO health (i.e. after every hit the bullet is destroyed). Further, if the UFO health has gone down to 0, then you need to destroy the actual UFO object. So your code would become something like this:

    function OnCollisionEnter(collision : Collision) { 
        if (collision.gameObject.tag == "Bullet") //if the tagged object is Bullet
        {
            UFOhealth = UFOhealth - 1; //takes away from the health I put above
            Destroy(collision.gameObject);
        }
    
        if (UFOhealth <=0)
        {
            Destroy(UFOobject, 3.0);
        }
    }
    

    Note that this is not for copy-paste, but just to give you an idea of where I think your mistake is.