Search Unity

killing my enemy causes problems:( please help:o

Discussion in 'Scripting' started by vorkas, Mar 29, 2013.

  1. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    I think I have confused my game! I have created my map my 3rd person camera(smooth follow on my character) I have created a prefab from a 3d model called bone(my prefab is called bonetoss) just so that I can throw bones. By using tornadotwins script on shooting I attached my bonetoss prefab to shoot a rotating bone(the above prefab).
    Anw the problem is when I shoot this bonetoss prefab to hit an enemy once that enemy is destroyed any other object I hit it gives me a null exception error-.-

    So the scripts I have that are probably are of interest are enemy script character movement script and bonetoss script

    Enemy script


    var speed : float=5.0;
    var lifeTime = 3.0;
    var health = 100;
    var domove : int = 0;


    function Awake () //unity predifined function
    {
    //Destroy (gameObject, lifeTime);//delete fireballs in "lifetime"/time set can also be changed from unity

    }
    function applydamage(damage:int)
    {
    Debug.Log("in applydamage"+health);
    health = health-damage;
    if (health ==0)
    {
    Destroy(gameObject);
    Debug.Log("newhealthis" +health);
    }
    }

    function Update()
    {
    if (domove==1)
    {
    transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
    //Debug.Log("update");
    animation.Play("soldier_run");
    /*audio.clip = walk;
    audio.Play("runbitch");*/
    }
    }

    function move () {

    domove =1;



    //transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
    //animation.Play("soldier_run");

    }
    Ignore the domove stuff thats basically attached to another script when I hit a box the enemy moves (that works:p)

    Bonetoss script:
    #pragma strict

    var lifeTime : int = 5.0;
    var damage : int= 100;
    var distance : float;
    var maxdistance : float = 10;
    var enemy :GameObject;

    function Awake () //unity predifined function
    {
    Destroy (gameObject, lifeTime);//delete fireballs in "lifetime"/time set can also be changed from unity

    }

    function Start ()
    {

    enemy = GameObject.FindWithTag("enemy");

    }

    function OnTriggerEnter(other : Collider)
    {
    if( GameObject.FindWithTag("enemy"))
    Debug.Log("you hit");
    enemy.SendMessage("applydamage",damage);
    }


    and character controller script:


    var rotationSpeed: float = 30;
    var movementSpeed: float = 30;
    var jumpingSpeed:float = 5;
    var isGrounded:boolean = true;
    var flashLight:Light;
    var bullitPrefab:Transform;
    var sprintSpeed =9;
    var damage : int= 50;
    var distance : float;
    var maxdistance : float = 10;



    var walk : AudioClip;



    function Start ()
    {

    /*
    flashLight = gameObject.Find("FlashLight").GetComponent(Light);*/
    }



    function Update ()

    {

    rigidbody.freezeRotation = true;



    transform.Translate(0,0,Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime);


    transform.Rotate(0,Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime,0);


    if(Input.GetButtonDown("Jump") isGrounded)
    {

    rigidbody.velocity.y+=jumpingSpeed;
    //animation.Play("zombie_jump");
    }

    /*if(Input.GetKeyDown(KeyCode.F))
    {

    if(flashLight.enabled)
    {
    flashLight.enabled=false;
    }

    else if(!flashLight.enabled)
    {
    flashLight.enabled=true;
    }


    }*/

    if(Input.GetKey(KeyCode.LeftShift))
    movementSpeed = sprintSpeed;
    else
    movementSpeed = 1;


    if(Input.GetKey(KeyCode.V))
    animation.Play("zombie_dance");


    if(Input.GetKeyDown("w"))
    {
    audio.clip = walk;
    audio.Play();
    }
    if(Input.GetKeyUp("w"))
    {
    audio.Stop();
    }

    if(Input.GetKeyDown("s"))
    {
    audio.clip = walk;
    audio.Play();
    }
    if(Input.GetKeyUp("s"))
    {
    audio.Stop();
    }



    if(Input.GetButtonDown("Fire1")) //checks if pressed jump basically means spacebar

    {
    var bullit = Instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position,Quaternion.identity);
    bullit.rigidbody.AddForce(transform.forward *2000);


    }

    }





    function OnCollisionStay(){
    isGrounded=true;

    }

    function onCollisionExit(){
    isGrounded=false;

    }
    I dunno if the last script is worth showing its how bonetoss is shot.
    I have tagged my enemy models to "enemy"
    I think my error is how I check for collision, by using debug.log it infact does show there is collision between the bone and the enemy. I feel like its the way I am asking unity to look for my enemies that is causing the problems :/
    Any help is well appreciated though

    My apologies for the extremely long post:(
     
  2. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    null reference exception is the error I get* and it is pointing it out in bonetoss script at this line: enemy.SendMessage("applydamage",damage);
     
  3. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    PLEASE put that in code tags.
     
  4. stridervan

    stridervan

    Joined:
    Nov 11, 2011
    Posts:
    141
    which means the enemy is never got assigned. do a debug statement before sending the message to see whether the enemy is null or actually assigned.
    this can happen if,
    * the enemy does't have the tag attached (most likely)
    * enemy isn't instantiated
     
  5. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    it should be
    Code (csharp):
    1.  
    2. function OnTriggerEnter(other : Collider)
    3. {
    4. if( other.CompareTag("enemy"))
    5. Debug.Log("you hit");
    6. other.transform.root.SendMessage("applydamage",damage); //to be safe
    7. /*  or use other.SendMessageUpwards("applydamage",damage);*/
    8. }
    9.  
    10.  
    You are already getting a trigger event, so you take the other guys collider that's hitting the trigger.
    Also, make sure you have a rigidbody on your gameObject, but set to Kinematic. Triggers don't work without them for some reason
     
  6. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    I tried to use the script you said but I am getting a different error which makes no sense to me:S now it says "SendMessage applydamage has no receiver!" I used both ways even the commented out one but it gives me this when I shoot twice:eek: made sure it was correctly written (function name) also when you said make sure you have a rigidbody on your gameObject (I have 1 an all moving objects but which one were you refering to specifically cause I have not played around with "kinematic" etc)
    Thank you for your response

    PS it will work on the enemy as before so he will take 2 hits and die but now if I shoot anywhere else I get the error:eek:
     
    Last edited: Mar 29, 2013
  7. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    I do apologize didn't notice them when I was creating this-.- cannot even find them now is it wrap
     
  8. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    can you give me a hint on how to debug for what as I am not the best of programmers and heavily rely on examples:/
     
  9. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    It's an exception message you get because anything else you hit that doesn't have the said function applied to it complains, you can set the receiver to
    Code (csharp):
    1.  
    2.    other.transform.root.SendMessage("applydamage",damage,SendMessageOptions.DontRequireReceiver );
    3.  
    4.  
    so it doesn't complain.
     
    Last edited: Mar 29, 2013
  10. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    this worked flawlessly but I do not understand what was happening and what we just changed:eek: could you possibly explain to me like I am a kid from kinder-garden or something:p just in plain terms what was I doing:pthe way I see it is if I have a collision go to the other script see how much health is left(basically cause I called that function) if zero health destory that object (talking about one with an enemy tag:p am I correct till here?:
     
  11. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    I think you're correct for the most part.

    Also,

    Code (csharp):
    1.  
    2. if (health ==0)
    3. {
    4. Destroy(gameObject);
    5. Debug.Log("newhealthis" +health);
    6. }
    You cannot call the debug function after you destroyed the WHOLE object!!! lol
     
  12. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    well sorry my bad debug stops when the function is destroyed:p I have noticed 1 more problem that is driving me crazy cause my enemy gets "destroyed". The scenario is if my character steps on a box the enemy gets called to move across the screen(if you noticed there was some domove stuff in enemy script:p). Problem is if I shoot the enemy and step on the object again I get : MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Is there a way for me to tell unity if the object is destroyed ignore this? again scripts involved is :

    Enemy Trigger:
    Code (csharp):
    1.  
    2. #pragma strict
    3. var speed : float=5.0;
    4. var enemy : GameObject;
    5. var walk : AudioClip;
    6.  
    7. function Start()
    8. {
    9.     Debug.Log("start");
    10.     enemy =  GameObject.FindWithTag("enemy");
    11.     Debug.Log("enemy is" + enemy.tag); 
    12.     //Debug.Log(GameObject.Find
    13.     //enemy.SendMessage("move");
    14. }
    15.  
    16. function OnTriggerEnter(other : Collider)
    17. {
    18.     if(enemy)
    19.     Debug.Log("ouch");
    20.    
    21.     //enemy = GameObject.Find("swat");
    22.     //Debug.Log("enemy is" + enemy.ToString);
    23.  
    24.     enemy.SendMessage("move",SendMessageOptions.DontRequireReceiver);
    25.    
    26.    
    27.     audio.clip = walk;
    28.     audio.Play();
    29.     //GameObject.Find("swat").SendMessage("move");
    30.  
    31.     //Debug.Log(enemy.ToString);
    32.     //enemy.
    33.     //enemy.transform.Translate(Vector3(0,0,(speed*Time.deltaTime)));
    34.    
    35.  
    36.         /*{
    37.         enemy.transform.Translate(Vector3(0,0,(speed*Time.deltaTime)));
    38.         //animation.Play("soldier_run");
    39.     }*/
    40. }
    41.  
    and enemy script :

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var speed : float=5.0;
    5. var lifeTime = 3.0;
    6. var health = 100;
    7. var domove : int = 0;
    8.  
    9.  
    10. function Awake () //unity predifined function
    11. {
    12. //Destroy (gameObject, lifeTime);//delete fireballs in "lifetime"/time set can also be changed from unity
    13.  
    14. }
    15. function applydamage(damage:int)
    16. {
    17. Debug.Log("in applydamage"+health);
    18.  health = health-damage;
    19. if (health ==0)
    20.     {
    21.         Destroy(gameObject);
    22.         Debug.Log("newhealthis" +health);
    23.     }
    24. }
    25.  
    26. function Update()
    27. {
    28.     if (domove==1)
    29.     {
    30.         transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
    31.         //Debug.Log("update");
    32.         animation.Play("soldier_run");
    33.         /*audio.clip = walk;
    34.         audio.Play("runbitch");*/
    35.     }
    36. }
    37.  
    38. function move () {
    39.  
    40. domove =1;
    41.  
    42.  
    43.  
    44. //transform.Translate(Vector3(0,0,speed)*Time.deltaTime);
    45. //animation.Play("soldier_run");
    46.  
    47. }
    48.  
    49.  
    50.  
    51.  
    I tried to add an if statement if health >=1 domove=1 (which would activate my characters movement only if his health is greater than 1) but it doesn't matter he gets called to move even if his health is 0 giving me the error that I am calling an object that is not existent basically:O
    and thank you again for all the help and attention:)
     
    Last edited: Mar 29, 2013