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. Dismiss Notice

Help with calling function from another script (Solved)

Discussion in 'Scripting' started by GameMaker81709, Jan 21, 2022.

  1. GameMaker81709

    GameMaker81709

    Joined:
    Jan 6, 2022
    Posts:
    4
    So i made a function called TakeDamage, in a script called PlayerHealth, and im trying to call it from another script. im getting the error : There is no argument given that corresponds to the required formal parameter 'damage' of 'PlayerHealth.TakeDamage(int)' Here is the function :
    public void TakeDamage(int damage)
    {
    currentHealth -= damage;

    healthBar.SetHealth(currentHealth);
    }
    and here is the script where im trying to call it :
    playerHealth.TakeDamage();
    Im fairly new to C# and unity, so any help will be appriciated
     
  2. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    12
    you need to call the script on the object by finding the script first. What i mean is you have not created an access point.

    If you are using a raycast then the code you would need is

    Code (CSharp):
    1. if(hit.transform.tag == Player)
    2. {
    3. hit.gameobject.GetComponent<PlayerHealth>().TakeDamage(int damage);
    4. }
    5. else
    6. {
    7.  
    8. }
    Just make sure your player has the tag of "Player". This should be correct, at work at the moment so cannot test.
     
  3. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    Up at the top of your script where you create variables, just like creating a variable, you create a reference to the script:
    Code (CSharp):
    1. public PlayerHealth playerhealth
    2.  
    3. void Start()
    4. {
    5.    playerhealth.TakeDamage();
    6. }

    FYI, you can determine how much damage while also calling the function:

    In PlayerHealth.cs:
    Code (CSharp):
    1. public float health;
    2.  
    3. public void TakeDamage(float damage)
    4. {
    5.    health -= damage;
    6. }
    7.  
    In the script that's calling TakeDamage():
    Code (CSharp):
    1. public PlayerHealth playerhealth
    2.  
    3. void Start()
    4. {
    5.    playerhealth.TakeDamage(10f);
    6. }
    I like to use a function like ModifyHealth(), so I can use it to add or subtract health for damage or healing:
    Code (CSharp):
    1. public float health;
    2.  
    3. public void ModifyHealth(float amount)
    4. {
    5.    health += amount;
    6. }
    7.  
    8. void Start()
    9. {
    10.   ModifyHealth(20f);
    11.   ModifyHealth(-20f);
    12. }
    13.  
     
    Putcho likes this.
  4. GameMaker81709

    GameMaker81709

    Joined:
    Jan 6, 2022
    Posts:
    4
    i changed the health varible to a float and made the function say
    Code (CSharp):
    1. public void TakeDamage(float damage)
    but now im getting the error
    Code (CSharp):
    1. error CS7036: There is no argument given that corresponds to the required formal parameter 'damage' of 'PlayerHealth.TakeDamage(float)'
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I answered your question here:

    https://forum.unity.com/threads/help-with-calling-function-from-another-script.1228047/#post-7827675

    Let me quote myself:

    The function in question requires an integer argument. Your call site supplies none. If that does not INSTANTLY make perfect sense to you and enable you to solve the problem, then set this code gently aside and start with some C# tutorials, such as the ones listed below.

    Go see the link above for the tutorials. Seriously. Unless you like pain, stop banging your head on this simple issue... it's as simple as holding a pen upside and not being able to write.
     
    knobblez likes this.
  6. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    Code (CSharp):
    1. public void TakeDamage(int damage)
    2.     {
    3.         currentHealth -= damage;
    4.  
    5.         healthBar.SetHealth(currentHealth);
    6.     }
    7.  
    and here is the script where im trying to call it :
    playerHealth.TakeDamage(5)<--;
    Leaving it empty is like if you just put healthBar.SetHealth() instead of healthBar.SetHealth(currentHealth).

    You need to specify how much damage.

    As Kurt said, this is very basic C#. Not trying to discourage you but rather encourage you to do some reading on the basics of C#.
    https://csharp.net-tutorials.com/basics/functions/

    It will benefit you to learn more of the basics so that you don't end up having to rewrite too much code because you approach it in a less efficient way.

    This is also good to know:
    https://answers.unity.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html
     
    Last edited: Jan 22, 2022
    Kurt-Dekker likes this.
  7. GameMaker81709

    GameMaker81709

    Joined:
    Jan 6, 2022
    Posts:
    4
    Nvm, i just placed my player health script on my Character object and solved my problem :rolleyes:
     
  8. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    Oof. You REALLY should learn how that works. It's simple and useful once you understand it.

    If every time you need to call a function from another script, you just combine them, you're gonna end up with a couple very long scripts lol. You do you but I recommend you learn about this.