Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with Damage script

Discussion in 'Scripting' started by Piracase, Mar 29, 2015.

  1. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Hello guyz,
    I'm working on a survival game and got many help from youtube but i get problems,I have a scipt named "EnemyLogic" Before i added animation and the weapon ,The Enemy damage stopped working



    Script code
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Health : int = 100;
    5.  
    6. function Update ()
    7. {
    8.     if (Health <= 0)
    9.     {        
    10.          Dead();
    11.     }
    12. }  
    13.  
    14. function ApplyDamage (TheDamage : int)
    15. {
    16.     Health -= TheDamage;
    17. }
    18.  
    19. function Dead()
    20. {
    21.     Destroy (gameObject);
    22. }
    23.  
    24.  
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Ummm.... You might need to provide more information. What exactly stopped? The death? Taking damage? Compiler errors?
     
  3. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Nothing,Just it won't let the enemies health go down :'(
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    What calls ApplyDamage? Is that still being called? Add a Debug.Log call in ApplyDamage to check.
     
  5. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Should i replace "ApplyDamage" to "Debug.Log "???
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    No, Change ApplyDamage to:

    Code (CSharp):
    1. function ApplyDamage (TheDamage : int)
    2. {
    3. Debug.Log("Hello from ApplyDamage");
    4. Health -= TheDamage;
    5. }

    Then run your game, attack stuff and check the unity console. There should be some hello messages there.
     
  7. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Nope,Nothing at all.
     
  8. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    And im using JAVASCRIPT
     
  9. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    No messages in the console? That means that method is never being called. Please show me the code that calls the ApplyDamage function.
     
  10. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Health : int = 100;
    5.  
    6.  
    7. function Update ()
    8. {
    9.     if (Health <= 0)
    10.     {        
    11.          Dead();
    12.     }
    13. }  
    14.  
    15. function ApplyDamage (TheDamage : int)
    16. {
    17.     Health -= TheDamage;
    18. }
    19.  
    20. function Dead()
    21. {
    22.     Destroy (gameObject);
    23. }
    24.  
    25.  
     
  11. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    ApplyDAMAGE Code

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. var Damage : int = 50;
    6. var Distance : float;
    7. var MaxDistance : float = 1.5;
    8. var TheMace : Transform;
    9.  
    10. function Update ()
    11. {  
    12. if (Input.GetButtonDown("Fire1"))
    13. {
    14.   TheMace.animation.Play("attack");
    15. var hit : RaycastHit;
    16. if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
    17. {
    18. Distance = hit.distance;
    19. if (Distance < MaxDistance)
    20. {
    21.     hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
    22.  }
    23.    }
    24.       }
    25.          }
    26.  
     
  12. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    At line 21
     
  13. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    After line 18 add:

    Debug.Log("I HIT AN ENEMY");

    The run and play and check the console again.
     
  14. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Yeah

    I HIT AN ENEMY
    UnityEngine.Debug:Log(Object)
    EnemyLogic:Main() (at Assets/EnemyLogic.js:18)



    now?
     
  15. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    ok, next add this line after line 20:
    Debug.Log("HE WAS IN RANGE");
     
  16. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Not working
     
  17. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    But When i replace "Debug.Log("I HIT AN ENEMY");" with "Debug.Log("HE WAS IN RANGE");"


    it works
     
  18. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Thats your problem. The range is too short. Set MaxDistance to 10 and try again.
     
  19. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    But When i replace "Debug.Log("I HIT AN ENEMY");" with "Debug.Log("HE WAS IN RANGE");"


    it works
     
  20. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Change

    var MaxDistance : float = 1.5;

    to

    var MaxDistance : float = 10;


    Your problem is that MaxDistance is too low
     
  21. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    I did,Nothing made change
     
  22. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Help me
     
  23. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I think the SendMessage is not working, try using GetComponent instead

    Code (CSharp):
    1.  
    2.  
    3. Distance = hit.distance;
    4. if (Distance < MaxDistance)
    5. {
    6.     hit.transform.GetComponent(script name).ApplyDamage(Damage);
    7. }
    8.    }
    9.       }
    10.          }
    Or try using gameObject.SendMessage instead of transform.SendMessage


    Code (CSharp):
    1.  
    2.  
    3. Distance = hit.distance;
    4. if (Distance < MaxDistance)
    5. {
    6.     hit.gameObject.SendMessage ("ApplyDamage", Damage);
    7. }
    8.    }
    9.       }
    10.          }
     
  24. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    (20,32): BCE0023: No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(UnityEngine.Transform)' was found.
     
  25. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Ok after line 18 add:

    Debug.Log(hit.distance);

    What is the value in the unity console?
     
  26. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Should i put in "MeleeSystem.js? " or "EnemyLogic.js" script
     
  27. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    just below this line.
    Distance = hit.Distance;
     
  28. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Assets/MeleeSystem.js(6,11): BCE0005: Unknown identifier: 'hit'.
     
  29. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Code (JavaScript):
    1.  
    2. #pragma strict
    3. var Damage : int = 50;
    4. var Distance : float;
    5. var MaxDistance : float = 1.5;
    6. var TheMace : Transform;
    7. function Update ()
    8. {
    9. if (Input.GetButtonDown("Fire1"))
    10. {
    11.   TheMace.animation.Play("attack");
    12. var hit : RaycastHit;
    13. if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
    14. {
    15. Distance = hit.distance;
    16. // HERE
    17. Debug.Log(hit.distance);
    18. if (Distance < MaxDistance)
    19. {
    20.     hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
    21. }
    22.    }
    23.       }
    24.          }
     
  30. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    UnassignedReferenceException: The variable TheMace of 'MeleeSystem' has not been assigned.
    You probably need to assign the TheMace variable of the MeleeSystem script in the inspector.
    MeleeSystem.Update () (at Assets/MeleeSystem.js:11)
     
  31. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    So assign the mace object in the inspector?

    I am getting the feeling you dont know what you are doing? And are just using someones scripts. You need to stop and learn the basics first.
     
  32. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    No i learned. i learned from backeys
     
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you been through the training sessions we have in the learn site?
    http://unity3d.com/learn/

    Do roll a ball and space shooter to get the basics down first.
     
  34. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    OK i'll do my best :)